BioWare Forums: View Post For Code Display


You are viewing a single post. Please do not take this post out of context.
Read the full topic: Post Working Scripts Here

Author Sticky: Post Working Scripts Here
Kaine Posted: Sunday, 23 June 02:08AM
Sheathing Weapons:
This is a small set of scripts I use for my city guards. I didn't think they'd normally walk around with their weapons drawn getting rained on. These scripts will have the NPCs draw their weapons before they run into combat then sheath them again after combat is finished.

The first part you can add to the NPCs existing OnPhysicalAttacked script, you could also drop it into an include file if you like.

NWScript:
int  GetBaseGroup(int nItem) {
//This 4 groups I use are:
// 0 = Misc items that can't be equiped as a weapon or shield
// 1 = Shields. I don't use this value right now but added it in case
//          someone wanted to equip a shield from inventory as well as
//          a weapon
// 2 = Weapons that can be sheathed or put away and are not normally
//          carried all the time in the hand.
// 3 = Weapons that are usually carried in the hand.
    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;
    //Weapons that can be sheathed or hook to the belt.
    case BASE_ITEM_BASTARDSWORD:
    case BASE_ITEM_BATTLEAXE:
    case BASE_ITEM_CLUB:
    case BASE_ITEM_DAGGER:
    case BASE_ITEM_DART:
    case BASE_ITEM_GREATSWORD:
    case BASE_ITEM_HANDAXE:
    case BASE_ITEM_KAMA:
    case BASE_ITEM_KATANA:
    case BASE_ITEM_KUKRI:
    case BASE_ITEM_LIGHTCROSSBOW:
    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_SHORTBOW:
    case BASE_ITEM_SHURIKEN:
    case BASE_ITEM_SICKLE:
    case BASE_ITEM_SLING:
    case BASE_ITEM_THROWINGAXE:
    case BASE_ITEM_WARHAMMER:
        nValue = 2;
        break;
    //Weapons that aren't sheathed
    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_MORNINGSTAR:
    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 ) ) > 1 ) {
                //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;
}


You then just need to put a call to EquipWeapon() in the main() of the OnPhysicalAttacked like this:

NWScript:
....
if( !GetIsObjectValid( GetAttemptedAttackTarget() ) && !GetIsObjectValid( GetAttemptedSpellTarget() ) ) {
 EquipWeapon();//<-- Add this line
 if( GetIsObjectValid( GetLastAttacker() ) ) {
....


Now your NPC should look for a weapon in inventory and equip it. Note, this doesnt do any checking to make sure there isnt a shield in the left hand and the NPC tries to equip a two handed weapon. If you want to put those checks in more power to you.

Okay, the monster is dead and our heroic city guard is standing there with his sword drawn in the rain. Now we want to have him put it away so it doesn't rust.

Now we drop a few lines into their OnHeartbeat script. Add or use the header file with the GetBaseGroup() function into the OnHeartbeat script as well as this function.
NWScript:
void PutWeaponAway() {
    if( !GetIsInCombat() ) {
        object objInHand;
        objInHand = GetItemInSlot( INVENTORY_SLOT_RIGHTHAND );
        if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 2 ) {
            ActionUnequipItem( objInHand );
        }
    }
}


Then in the main() of the OnHeartbeat add the a call to the new function like this

NWScript:
void main(){
    PutWeaponAway( ); //<----Add this line
    if( GetSpawnInCondition( NW_FLAG_FAST_BUFF_ENEMY ) )
    {
....

I've run this with about 20 guards at a time fighting monsters and using this script at a time and it didnt make any noticable lag. Since it is in the OnHeartbeat you may want to test it on your system.

...and the "Could Not Submit: Message contains word with too many characters without a space. Please check your message" sucks for this forum btw.

[ Edited By Kaine: Sunday, 23 June 02:08AM (GMT) ]

[ Edited By Kaine: Sunday, 23 June 02:09AM (GMT) ]

This post taken from the BioWare Forums
Powered by BioBoards Version 2.05.006
Please read our Copyright and Trademark Information