| Author: | Rehevkor |
| Author Website: | n/a |
| Script Name: | NPC Weapons Equip/Unequip (Version 3) |
| Description: | This script allows you to manage when weapons are equipped and removed on NPCs. It supports single, two handed, and dual wielded weapons, weapon and shield, and the use of torches. |
| Instructions on Use: | The NPCs will draw what you tell them to when battle begins, then sheath
the weapons when it is over. Also, if you enable the use of torches in the
script, they will draw these at the appropriate times, and remove them
during battle.
I added a fix in this version that allows the script to work properly on NPCs who are walking waypoints. A more in-depth description and full instructions on how to implement the entire system are provided in the comments section at the start of the file. There are still some issues with dual-wielding that I have yet to solve, but everything else works (at least for me). |
Script 1:
//:://///////////////////////////////////////////// //:: NPC Weapon Equip/Unequip Include File //:: NPC_EQUIP //:: Release 3 //::////////////////////////////////////////////// //::////////////////////////////////////////////// //:: Created By: Rehevkor, base script by Kaine //:: Created On: July 1, 2002 //:: Updated On: July 11, 2002 //:: jcestep17@hotmail.com //::////////////////////////////////////////////// /* **CHANGES FROM LAST RELEASE** Fixed using WalkWaypoints with this script! It was a massively frustrating effort, but I figured it out. The biggest problem was a call to WalkWaypoints in the default OnHeartbeat code that I hadn't noticed before. It would fire in the middle of my script, effectively cutting it off. Instructions on how to enable walking waypoints with this script are below. Also, this should work with the standard WalkWaypoints as well as any custom versions of it. I also added GetIsRightHandEmpty() and GetIsLeftHandEmpty() to clean up the if statements a bit. You won't need to use these functions yourself unless you have an idea that they would help with. ***************************** 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()); DelayCommand(1.0, PutShieldAway()); } //////////////////////////////////////////////////////////////////////////// **NOTE** If you intend to use the script on an NPC that is walking waypoints, then you MUST make the following change to the default code in OnHeartbeat as well as adding the lines above. In the original nw_c2_default1 OnHeartbeat script, at line 44, there is a call to WalkWayPoints that must be removed. Either delete it or comment it out, then add the code above to complete the OnHeartbeat script. 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(... //////////////////////////////////////////////////////////////////////////// OnHeartbeat: Equipping/Removing a Torch This is the reccomended method for doing this, although you're welcome to use the functions to handle torches however you see fit. To remove a torch when there are enemies about, simply place the PutTorchAway() call at the top of each event other than OnHeartbeat outlined above. The code below will re-equip the torch when the enemies are gone. //////////////////////////////////////////////////////////////////////////// if (GetIsInCombat() == FALSE) { ClearAllActions(); DelayCommand(1.0, PutWeaponAway()); DelayCommand(1.0, PutShieldAway()); if(GetIsNight() == TRUE) { EquipTorch(); } else if(GetIsDay() == TRUE) { PutTorchAway(); } } //////////////////////////////////////////////////////////////////////////// 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 :) */ #include "NW_I0_GENERIC" int GetBaseGroup(int nItem) { // 0 - Non-equippable items // 1 - Shields // 2 - One handed weapons // 3 - Two handed weapons // 4 - Other 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; case BASE_ITEM_TORCH: nValue = 4; } return nValue; } int GetIsRightHandEmpty() { object objInHand = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND); if(objInHand == OBJECT_INVALID) { return TRUE; } else return FALSE; } int GetIsLeftHandEmpty() { object objInHand = GetItemInSlot(INVENTORY_SLOT_LEFTHAND); if(objInHand == OBJECT_INVALID) { return TRUE; } else return FALSE; } void EquipWeapon() { if( GetIsRightHandEmpty() == TRUE ) { object objInv; //start getting the inventory items objInv = GetFirstItemInInventory(); //If there was no inventory item to get quit looking. while( objInv != OBJECT_INVALID ) { if( GetBaseGroup( GetBaseItemType( objInv ) ) == 2 ) { //Found a weapon so equip it ActionEquipItem( objInv, INVENTORY_SLOT_RIGHTHAND ); break; } objInv = GetNextItemInInventory(); } } return; } void EquipOffWeapon() { if( GetIsLeftHandEmpty() == TRUE ) { object objInv; //start getting the inventory items objInv = GetFirstItemInInventory(); //If there was no inventory item to get quit looking. while( objInv != OBJECT_INVALID ) { if( GetBaseGroup( GetBaseItemType( objInv ) ) == 2 ) { //Found a weapon so equip it ActionEquipItem( objInv, INVENTORY_SLOT_LEFTHAND ); break; } objInv = GetNextItemInInventory(); } } return; } void EquipTwoHandWeapon() { if( GetIsRightHandEmpty() == TRUE ) { 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; } objInv = GetNextItemInInventory(); } } return; } void PutWeaponAway() { int nRemoved = 0; object objInHand; objInHand = GetItemInSlot( INVENTORY_SLOT_RIGHTHAND ); if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 2 || GetBaseGroup( GetBaseItemType( objInHand ) ) == 3) { ActionUnequipItem( objInHand ); nRemoved = 1; } objInHand = GetItemInSlot( INVENTORY_SLOT_LEFTHAND ); if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 2) { DelayCommand(1.0, ActionUnequipItem( objInHand )); nRemoved = 1; } if(nRemoved == 1) { DelayCommand(2.0, WalkWayPoints()); } return; } void EquipTorch() { if( GetIsLeftHandEmpty() == TRUE ) { object objInv; //start getting the inventory items objInv = GetFirstItemInInventory(); //If there was no inventory item to get quit looking. while( objInv != OBJECT_INVALID ) { if( GetBaseGroup( GetBaseItemType( objInv ) ) == 4 ) { ActionEquipItem( objInv, INVENTORY_SLOT_LEFTHAND ); break; } objInv = GetNextItemInInventory(); } } return; } void PutTorchAway() { if( GetIsObjectValid( GetItemInSlot( INVENTORY_SLOT_LEFTHAND ) ) ) { object objInHand; objInHand = GetItemInSlot( INVENTORY_SLOT_LEFTHAND ); if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 4) { DelayCommand(0.5, ActionUnequipItem( objInHand )); } return; } } void EquipShield() { if( GetIsLeftHandEmpty() == TRUE ) { 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; } objInv = GetNextItemInInventory(); } } return; } void PutShieldAway() { object objInHand; objInHand = GetItemInSlot( INVENTORY_SLOT_LEFTHAND ); if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 1) { DelayCommand(1.0, ActionUnequipItem( objInHand )); } return; } |