From c3bd5a0ce004fa86b06e3d1ef38f3fb3a87cd4fa Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 22 Nov 2026 02:08:41 +0000 Subject: [PATCH] fix: a script that takes the last piece of a worn item takes it off - giving Greg your farmer clothes leaves you in your naked body Like the engine (oCNpc::RemoveFromInv, OpenGothic Inventory::delItem), Npc_RemoveInvItem(s) unequips an equipped item when its last piece leaves the inventory: protection and on_unequip (next frame, like script equips) go, the armor visual (VR hero body) goes back to the naked body. Weapons leave the equipped list and the NPC's belt. Only script removals: a VR weapon in a hand or holster is out of the backpack but still equipped. --- .../Domain/Vm/VmNpcInventoryExternalDomain.cs | 2 ++ .../Services/Npc/NpcInventoryService.cs | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) 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) {