NSActor: add SwitchToBestWeapon(bool ignoreCurrent)

This commit is contained in:
Marco Cawthorne 2024-07-31 17:28:20 -07:00
parent 0adf97e2de
commit c3f527d5e5
Signed by: eukara
GPG key ID: CE2032F0A2882A22
2 changed files with 54 additions and 7 deletions

View file

@ -110,6 +110,7 @@ public:
nonvirtual void SwitchToWeapon(string);
/** Switches the entity to use the desired weapon. */
nonvirtual void SwitchToExactWeapon(NSWeapon);
nonvirtual void SwitchToBestWeapon(bool);
nonvirtual void LaunchProjectile(string, bool, float);
nonvirtual bool PlantCharge(string);

View file

@ -746,7 +746,7 @@ NSActor::GiveItem(string itemName)
/* TODO: Replace this with a 'SwitchBest' type function? */
/* we have no active weapon, and this is our pick */
if (!m_activeWeapon) {
SwitchToExactWeapon((NSWeapon)linkedList);
SwitchToBestWeapon(false);
}
/* items (not weapons) may not be added permanently by default */
@ -760,6 +760,53 @@ NSActor::GiveItem(string itemName)
#endif
}
void
NSActor::SwitchToBestWeapon(bool ignoreActive)
{
string toIgnore = __NULL__;
NSItem linkedList = __NULL__;
NSItem bestWeapon = __NULL__;
float bestWeight = 0.0f;
if (ignoreActive && m_activeWeapon) {
toIgnore = m_activeWeapon.classname;
}
/* we posess nothing. auto return false. */
if (!m_itemList) {
return;
}
/* iterate over all the inventory items */
linkedList = m_itemList;
while (linkedList) {
/* inventory item may not be a weapon */
if (linkedList.IsWeapon()) {
string weaponName = linkedList.classname;
float weight;
if (toIgnore == weaponName) {
linkedList = linkedList.chain;
continue;
}
weight = stof(EntityDef_GetKeyValue(weaponName, "weight"));
if (weight > bestWeight) {
bestWeight = weight;
bestWeapon = linkedList;
}
}
linkedList = linkedList.chain;
}
if (bestWeapon) {
SwitchToExactWeapon(bestWeapon);
}
}
bool
NSActor::RemoveItem(string itemName)
{
@ -775,6 +822,11 @@ NSActor::RemoveItem(string itemName)
return (false);
}
/* prevent nonsense */
if (GetCurrentWeapon() == itemName) {
SwitchToBestWeapon(true);
}
/* since we have something in the inventory, start there */
linkedList = m_itemList;
@ -793,12 +845,6 @@ NSActor::RemoveItem(string itemName)
/* successfully remove the last item */
if (removeItem == true) {
/* Is the item our active weapon? Handle graceful removal. */
if (m_activeWeapon == itemToRemove) {
m_activeWeapon._SwitchedFromCallback();
m_activeWeapon = __NULL__;
}
/* we had an item in front, bridge across the removed item. */
if (frontItem) {
frontItem.chain = (NSItem)itemToRemove.chain;