Skip to content

Commit 0f5f56a

Browse files
authored
preserve inventory order in AHuman::EquipFirearm
This prevents AIs from messing up the inventory order whenever a player switches away from a unit while holding a tool or something. This has been bugging me for months.
1 parent 67b74c5 commit 0f5f56a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Source/Entities/AHuman.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,19 @@ bool AHuman::EquipFirearm(bool doEquip) {
648648
// Found proper device to equip, so make the switch!
649649
if (pWeapon && pWeapon->IsWeapon()) {
650650
if (doEquip) {
651-
// Erase the inventory entry containing the device we now have switched to
652-
*itr = 0;
653-
m_Inventory.erase(itr);
654-
655651
// Put back into the inventory what we had in our hands, if anything
656652
if (HeldDevice* heldDevice = m_pFGArm->GetHeldDevice()) {
657653
heldDevice->Deactivate();
658654
AddToInventoryBack(m_pFGArm->RemoveAttachable(heldDevice));
659655
}
660656

657+
// We want to preserve inventory order, so rotate it to the device in question.
658+
std::rotate(m_Inventory.begin(), itr, m_Inventory.end());
659+
660+
// Erase the inventory entry containing the device we now have switched to
661+
*m_Inventory.begin() = 0;
662+
m_Inventory.pop_front();
663+
661664
// Now put the device we were looking for and found into the hand
662665
m_pFGArm->SetHeldDevice(pWeapon);
663666
// Move the hand to a poisition so it looks like the new device was drawn from inventory
@@ -670,6 +673,7 @@ bool AHuman::EquipFirearm(bool doEquip) {
670673
if (m_DeviceSwitchSound) {
671674
m_DeviceSwitchSound->Play(m_Pos);
672675
}
676+
673677
}
674678

675679
return true;

0 commit comments

Comments
 (0)