diff --git a/Assets/Gothic-Core/Scripts/Domain/Vm/VmNpcInventoryExternalDomain.cs b/Assets/Gothic-Core/Scripts/Domain/Vm/VmNpcInventoryExternalDomain.cs index 0b8f39dc9..ff4d331f8 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Vm/VmNpcInventoryExternalDomain.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Vm/VmNpcInventoryExternalDomain.cs @@ -106,6 +106,7 @@ public void CreateInvItems(NpcInstance npc, int itemId, int amount) public void Npc_RemoveInvItem(NpcInstance npc, int itemId) { _npcInventoryService.ExtRemoveInvItems(npc, itemId, 1); + _npcInventoryService.UnequipIfLastOneRemoved(npc, itemId); GlobalEventDispatcher.ScriptRemovedInvItems.Invoke(npc.GetUserData(), itemId, 1); _support.LogInstantExternal(nameof(Npc_RemoveInvItem), npc, itemId); } @@ -113,6 +114,7 @@ public void Npc_RemoveInvItem(NpcInstance npc, int itemId) public void Npc_RemoveInvItems(NpcInstance npc, int itemId, int amount) { _npcInventoryService.ExtRemoveInvItems(npc, itemId, amount); + _npcInventoryService.UnequipIfLastOneRemoved(npc, itemId); GlobalEventDispatcher.ScriptRemovedInvItems.Invoke(npc.GetUserData(), itemId, amount); _support.LogInstantExternal(nameof(Npc_RemoveInvItems), npc, itemId, amount); } diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcInventoryService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcInventoryService.cs index ac82442fd..24b52fa0c 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcInventoryService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcInventoryService.cs @@ -158,6 +158,31 @@ public void EquipBestArmor(NpcInstance npc) EquipArmorFromInventory(npc, best); } + /// + /// Like the engine (oCNpc::RemoveFromInv, OpenGothic Inventory::delItem): a script taking the last piece of an + /// equipped item takes it off first. G2's Greg gets the hero's farmer clothes (B_GiveInvItems): the hero's + /// protection and the item's on_unequip go, and the hero's body shows the naked body again. + /// Only for script removals (Npc_RemoveInvItem(s)): a VR weapon held in a hand or holster is out of the + /// backpack but still equipped. + /// + public void UnequipIfLastOneRemoved(NpcInstance npc, int itemIndex) + { + var container = npc?.GetUserData(); + var item = _vmCacheService.TryGetItemData(itemIndex); + if (container?.Props == null || item == null || ExtNpcHasItems(npc, itemIndex) > 0 || + container.Props.EquippedItems.All(i => i?.Index != item.Index)) + return; + + Logger.Log($"[Equip] {npc.GetName(NpcNameSlot.Slot0)}: a script took the last '{item.Name}' - taking it " + + "off like the engine.", LogCat.Npc); + + // Externals run inside a script: on_unequip runs next frame, like the script equips (ExtEquipItem). + if (item.StaysEquippedOutsideBackpack()) + _equipDomain.RemoveEquippedWeapon(container, item); + else + _equipDomain.UnequipItemWithEffects(npc, item, deferItemFunctions: true); + } + /// public void RemoveEquippedWeapon(NpcContainer npc, string itemInstanceName) {