This was from the old NWVault, and I can't seem to find it on the new vault so I don't know who to give credit for it. But this should work (although I haven't tested it now in probably a few years) -
Put this or merge this into the Module OnEquip:
//stopswap_onequ
#include "stopswap_inc"
void main()
{
object oItem = GetPCItemLastEquipped();
object oPC = GetPCItemLastEquippedBy();
//Stop Swapping of weapons in combat with no penalties (see stopswap_XXX scripts for more details)
int nSlot = GetRestictedInventorySlot(oItem);
if (GetLocalObject(oPC, VAR_SLOT_KEY + IntToString(nSlot)) == oItem) {
// unlock the slot
DeleteLocalInt(oPC, VAR_SLOT_LOCK + IntToString(nSlot));
DeleteLocalObject(oPC, VAR_SLOT_KEY + IntToString(nSlot));
}
}
Put this or merge this into the Module OnUnequip:
//stopswap_onunequ
#include "stopswap_inc"
// -----------------------------------------------------------------------------
// MAIN
// -----------------------------------------------------------------------------
// module event - OnPlayerUnEquipItem
// file name - stopswap_onunequ v1.3 (added tag based scripting)
void main()
{
object oItem = GetPCItemLastUnequipped();
object oPC = GetPCItemLastUnequippedBy();
// pre-emptive abort: if the PC's appearance is not a standard PC appearance type the PC is/has just
// polymorphed which merges inventory slots so no need for restrictions on swapping items
if(GetAppearanceType(oPC) > LAST_PC_APPEARANCE_TYPE && GetSubRace(oPC) == "") return;
if(GetCurrentHitPoints(oPC) <= 1) return; //If the PC is dying or dead, let them unequip (usually forced by the module)
// Check if PC is in combat & within the unsafe distance, & if they equip a
// melee or ranged weapon if so, create an attack of opportunity
if(GetWasWeaponUnequippedInMelee(oPC, oItem) && UNEQUIP_WEAPON_PROVOKES_AOO && !GetLocalInt(oPC, "SwappedWeapons")) {
ActionStepBackwards(oPC, 2.5);
SendMessageToPC(oPC, "It takes you a moment to switch weapons while in melee combat!");
SetLocalInt(oPC, "SwappedWeapons", 1);
DelayCommand(0.1, DeleteLocalInt(oPC, "SwappedWeapons"));
DelayCommand(0.1, AssignCommand(oPC, ClearAllActions(TRUE)));
DelayCommand(0.1, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectCutsceneParalyze(), oPC, 1.5));
}
else if(GetIsOkayToUnequip(oPC, oItem) == FALSE) {
int nSlot = GetRestictedInventorySlot(oItem);
// lock the slot and note the key
SetLocalInt(oPC, VAR_SLOT_LOCK + IntToString(nSlot), TRUE);
SetLocalObject(oPC, VAR_SLOT_KEY + IntToString(nSlot), oItem);
// force the PC to re-equip the item
AssignCommand(oPC, ActionEquipItem(oItem, nSlot));
SendMessageToPC(oPC, "You can't unequip " + GetName(oItem) + " in melee combat!");
}
}
And finally, create an include script called stopswap_inc with this in it:
// file name - stopswap_inc v1.3
#include "x2_inc_itemprop"
// -----------------------------------------------------------------------------
// CONSTANTS
// -----------------------------------------------------------------------------
const float SAFE_DISTANCE = 4.0; // Set safe distance to equip items during combat
const int LAST_PC_APPEARANCE_TYPE = 6; // Last standard racial type
const int UNEQUIP_WEAPON_PROVOKES_AOO = TRUE; // TRUE or FALSE
const string VAR_SLOT_KEY = "SLOT_KEY_";
const string VAR_SLOT_LOCK = "SLOT_LOCK_";
// -----------------------------------------------------------------------------
// PROTOTYPES
// -----------------------------------------------------------------------------
int GetIsRestrictedSlot(int nSlot);
int GetRestictedInventorySlot(object oItem);
int GetRestrictedInventorySlotByType(int nType);
// Check if PC is in combat & within the unsafe distance,
// & if they equip a melee or ranged weapon if so,
// create an attack of opportunity
// oPC - Player
// oItem - Check if melee or ranged weapon
int GetWasWeaponUnequippedInMelee(object oPC, object oItem);
int GetIsOkayToUnequip(object oPC, object oItem);
void ActionStepBackwards(object oCreature, float fDistance);
// -----------------------------------------------------------------------------
// FUNCTIONS
// -----------------------------------------------------------------------------
int GetIsRestrictedSlot(int nSlot)
{
return nSlot > -1;
}
int GetRestictedInventorySlot(object oItem)
{
return GetRestrictedInventorySlotByType(GetBaseItemType(oItem));
}
int GetRestrictedInventorySlotByType(int nType)
{
int nRet = -1;
switch (nType)
{
case BASE_ITEM_AMULET: nRet = INVENTORY_SLOT_NECK; break;
case BASE_ITEM_BELT: nRet = INVENTORY_SLOT_BELT; break;
case BASE_ITEM_BOOTS: nRet = INVENTORY_SLOT_BOOTS; break;
case BASE_ITEM_CLOAK: nRet = INVENTORY_SLOT_CLOAK; break;
case BASE_ITEM_BRACER:
case BASE_ITEM_GLOVES: nRet = INVENTORY_SLOT_ARMS; break;
case BASE_ITEM_HELMET: nRet = INVENTORY_SLOT_HEAD; break;
case BASE_ITEM_LARGESHIELD:
case BASE_ITEM_SMALLSHIELD:
case BASE_ITEM_TOWERSHIELD: nRet = INVENTORY_SLOT_LEFTHAND; break;
}
return nRet;
}
int GetWasWeaponUnequippedInMelee(object oPC, object oItem)
{
// is it a weapon?
if(IPGetIsMeleeWeapon(oItem) || IPGetIsRangedWeapon(oItem))
{
// is the PC in combat?
if(GetIsInCombat(oPC))
{
// is the nearest enemy in striking range?
object oEnemy = GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, oPC);
if(GetDistanceBetween(oPC, oEnemy) < SAFE_DISTANCE)
{
return TRUE;
}
}
}
return FALSE;
}
int GetIsOkayToUnequip(object oPC, object oItem)
{
int nSlot = GetRestictedInventorySlot(oItem);
// is the slot locked?
// NOTE: if we are trying to unequip something from a locked slot then it
// is the system doing the unequipping
if(GetLocalInt(oPC, VAR_SLOT_LOCK + IntToString(nSlot)))
{
return TRUE;
}
// is it a restricted slot?
if(GetIsRestrictedSlot(nSlot))
{
// is PC in combat
if(GetIsInCombat(oPC))
{
// is enemy too close
object oEnemy = GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, oPC);
if(GetDistanceBetween(oPC, oEnemy) < SAFE_DISTANCE)
{
return FALSE;
}
}
}
// at least one condition was not met so it is okay to unequip
return TRUE;
}
void ActionStepBackwards(object oCreature, float fDistance)
{
// decompose the creature's current location
object oArea = GetArea(oCreature);
vector vPosition = GetPosition(oCreature);
float fFacing = GetFacing(oCreature);
// generate a position behind the creature
float fX = fDistance * cos(fFacing + 180.0);
float fY = fDistance * sin(fFacing + 180.0);
vector vBackwards = Vector(fX, fY);
// generate a location behind the creature
location lBackwards = Location(oArea, vPosition + vBackwards, fFacing);
// force the creature to run there
AssignCommand(oCreature, ClearAllActions());
AssignCommand(oCreature, ActionMoveToLocation(lBackwards, TRUE));
}
Editted becaues these boards suck for formatting code.