Equip best equipment action script

@rjshae This might be waht you are looking for. Very promising in tests.

// ga_equip_best
/*
	Makes the sCharacterTag equip the best equipment in their inventory for all slots.
		Some is based on item value.
	
	sCharacterTag = Tag of the character to equip. If blank, the owner is used.
	nPosture	0 = Default. Equip the best weapon. Also equip the best shield if a one-hand weapon is equipped. 
				1 = Only equip the best shield and the best one-hand weapon.
				2 = Only equip the two best dual-wield weapons.
				3 = Only equip the best missile weapon. Checks to equip a shield with one-hand missile weapons. 
*/
// travus 5/18/21

#include "ginc_param_const"
#include "x0_i0_match"

int IsEquippable(object oTarget, object oItem)
{
	int nType = GetBaseItemType(oItem);
	string sResult0 = Get2DAString("baseitems", "ReqFeat0", nType);
	string sResult1 = Get2DAString("baseitems", "ReqFeat1", nType);
	string sResult2 = Get2DAString("baseitems", "ReqFeat2", nType);
	string sResult3 = Get2DAString("baseitems", "ReqFeat3", nType);
	string sResult4 = Get2DAString("baseitems", "ReqFeat4", nType);
	string sResult5 = Get2DAString("baseitems", "ReqFeat5", nType);
	
	if ((GetHasFeat(StringToInt(sResult0), oTarget) || GetHasFeat(StringToInt(sResult1), oTarget) ||
		GetHasFeat(StringToInt(sResult2), oTarget) || GetHasFeat(StringToInt(sResult3), oTarget) ||
		GetHasFeat(StringToInt(sResult4), oTarget) || GetHasFeat(StringToInt(sResult5), oTarget)) &&
		GetIdentified(oItem))
	{
		return TRUE;
	}
	
	return FALSE;
}

void EquipBestShield(object oTarget)
{
	object oItem = GetFirstItemInInventory(oTarget);
	
	while (GetIsObjectValid(oItem))
	{
		if (MatchShield(oItem))
		{
			if (GetGoldPieceValue(oItem) > GetGoldPieceValue(GetLocalObject(oTarget, "Shield")) &&
				IsEquippable(oTarget, oItem))
			{
				SetLocalObject(oTarget, "Shield", oItem);
			}
		}
		oItem = GetNextItemInInventory(oTarget);
	}
	
	AssignCommand(oTarget, ActionEquipItem(GetLocalObject(oTarget, "Shield"), INVENTORY_SLOT_LEFTHAND));	
}

void EquipBestSingleMelee(object oTarget, int nOffhand=FALSE)
{
	object oItem = GetFirstItemInInventory(oTarget);
	int nHand = INVENTORY_SLOT_RIGHTHAND;	
	string sName = "MainHand";
	
	if (nOffhand)
	{
		nHand = INVENTORY_SLOT_LEFTHAND;
		sName = "OffHand";
	}
		
	while (GetIsObjectValid(oItem))
	{
		if (MatchSingleHandedWeapon(oItem))
		{
			if (GetGoldPieceValue(oItem) > GetGoldPieceValue(GetLocalObject(oTarget, sName)) &&
				IsEquippable(oTarget, oItem))
			{
				SetLocalObject(oTarget, sName, oItem);
			}
		}
		oItem = GetNextItemInInventory(oTarget);
	}
	
	AssignCommand(oTarget, ActionEquipItem(GetLocalObject(oTarget, sName), nHand));
}

void EquipBestItem(object oTarget, int nBase, int nSlot)
{
	object oItem = GetFirstItemInInventory(oTarget);	
	string sName = IntToString(nBase) + IntToString(nSlot);
	
	DeleteLocalObject(oTarget, sName);
	
	while (GetIsObjectValid(oItem))
	{
		if (GetBaseItemType(oItem) == nBase)
		{
			if (GetGoldPieceValue(oItem) > GetGoldPieceValue(GetLocalObject(oTarget, sName)) &&
				GetIdentified(oItem))
			{
				SetLocalObject(oTarget, sName, oItem);
			}
		}
		oItem = GetNextItemInInventory(oTarget);
	}
	
	AssignCommand(oTarget, ActionEquipItem(GetLocalObject(oTarget, sName), nSlot));
}

void EquipBestMiscItems(object oTarget)
{
	AssignCommand(oTarget, ActionEquipMostEffectiveArmor());
	EquipBestItem(oTarget, BASE_ITEM_BRACER, INVENTORY_SLOT_ARMS);
	EquipBestItem(oTarget, BASE_ITEM_ARROW, INVENTORY_SLOT_ARROWS);	
	EquipBestItem(oTarget, BASE_ITEM_BELT, INVENTORY_SLOT_BELT);
	EquipBestItem(oTarget, BASE_ITEM_BOLT, INVENTORY_SLOT_BOLTS);
	EquipBestItem(oTarget, BASE_ITEM_BOOTS, INVENTORY_SLOT_BOOTS);
	EquipBestItem(oTarget, BASE_ITEM_BULLET, INVENTORY_SLOT_BULLETS);
	EquipBestItem(oTarget, BASE_ITEM_CLOAK, INVENTORY_SLOT_CLOAK);
	EquipBestItem(oTarget, BASE_ITEM_HELMET, INVENTORY_SLOT_HEAD);
	EquipBestItem(oTarget, BASE_ITEM_RING, INVENTORY_SLOT_LEFTRING);
	EquipBestItem(oTarget, BASE_ITEM_AMULET, INVENTORY_SLOT_NECK);
	DelayCommand(0.2f, EquipBestItem(oTarget, BASE_ITEM_RING, INVENTORY_SLOT_RIGHTRING));
}

void CheckShield(object oTarget)
{
	object oRHand = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oTarget);
	
	if (!MatchNormalBow(oRHand) && !MatchCrossbow(oRHand))
	{
		EquipBestShield(oTarget);
	}
}

void CheckOneHand(object oTarget)
{
	object oRHand = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oTarget);
	
	if (MatchSingleHandedWeapon(oRHand))
	{
		EquipBestShield(oTarget);
	}
}

void main(string sCharacterTag, int nPosture)
{
	object oTarget = GetTarget(sCharacterTag);
	int n;
	
	DeleteLocalObject(oTarget, "Shield");
	DeleteLocalObject(oTarget, "MainHand");
	DeleteLocalObject(oTarget, "OffHand");

	for (n = INVENTORY_SLOT_HEAD; n < INVENTORY_SLOT_CWEAPON_L; n++)
	{
		AssignCommand(oTarget, ActionUnequipItem(GetItemInSlot(n)));
	}	
		
	DelayCommand(0.2f, EquipBestMiscItems(oTarget));			

	switch (nPosture)
	{
		case 0:
			DelayCommand(0.6f, AssignCommand(oTarget, ActionEquipMostDamagingMelee()));
			DelayCommand(0.8f, CheckOneHand(oTarget));
			break;
	
		case 1:
			DelayCommand(0.6f, EquipBestSingleMelee(oTarget));
			DelayCommand(0.8f, EquipBestShield(oTarget));
			break;
	
		case 2:
			DelayCommand(0.6f, EquipBestSingleMelee(oTarget));
			DelayCommand(0.8f, EquipBestSingleMelee(oTarget, TRUE));
			break;
	
		case 3:
			DelayCommand(0.6f, AssignCommand(oTarget, ActionEquipMostDamagingRanged()));			
			DelayCommand(0.8f, CheckShield(oTarget));
			break;
			
		default:
			DelayCommand(0.6f, AssignCommand(oTarget, ActionEquipMostDamagingMelee()));
			DelayCommand(0.8f, CheckOneHand(oTarget));
			break;	
	}
}
6 Likes

Hey @travus

May I use this for some possible companion AI tweaks?

Of course! Do with it as you please :slight_smile:

2 Likes