From 17d8e261b879ec39b80fb2108aefa86e05e9db58 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:13:37 +0000 Subject: [PATCH 1/8] fix: smiths hold the hammer at the anvil again - the model script's DEF_CREATE_ITEM puts its item into the hand T_BSANVIL_S0_2_S1 (HUMANS.MDS) inserts the hot blade (ItemInsert, the mob's item) into the left hand and creates the hammer ITMW_1H_MACE_L_04 in the right one (ItemCreate). ItemCreate fell through to "not yet supported", so Harad (G2) and the G1 smiths forged without a hammer. The created item's mesh goes into its slot and leaves together with the used item (T_BSANVIL_S1_2_S0's ItemDestroy, or the item put away when the NPC is interrupted). The inventory doesn't change. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/Animations/AnimationSystem.cs | 3 + .../Adapters/Properties/NpcProperties.cs | 3 + .../Scripts/Services/Npc/NpcService.cs | 62 ++++++++++++++++--- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs index ad2b69016..1686387cb 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs @@ -881,6 +881,9 @@ private void ApplyEventTags(AnimationTrackInstance trackInstance) case EventType.ItemInsert: _npcService.InsertItem(NpcData, eventTag.Slots.Item1, eventTag.Slots.Item2); break; + case EventType.ItemCreate: + _npcService.CreateItem(NpcData, eventTag.Slots.Item1, eventTag.Item); + break; case EventType.ItemDestroy: case EventType.ItemRemove: // The slot is cleared too - a stale one could later delete a drawn weapon. diff --git a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs index 31d2f4a7c..077c3afaf 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs @@ -27,6 +27,9 @@ public class NpcProperties // NPC items/talents/... public List EquippedItems = new(); public string UsedItemSlot; + // The slot of an item a model script created (DEF_CREATE_ITEM, e.g. the smith's hammer at the anvil). It goes + // together with the used item (DEF_DESTROY_ITEM, the item put away). + public string CreatedItemSlot; public bool HasItemEquipped; public int CurrentItem = -1; // We need to start with an "invalid" value as >0< is an allowed state value like in >t_Potion_Stand_2_S0< diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 78a44b9ea..83a1ede19 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -906,7 +906,8 @@ public void RemoveUsedItemFromHand(NpcContainer npcContainer) var props = npcContainer?.Props; if (props == null) return; - if (string.IsNullOrEmpty(props.UsedItemSlot) && !props.HasItemEquipped && props.ItemAnimationState < 0) + if (string.IsNullOrEmpty(props.UsedItemSlot) && string.IsNullOrEmpty(props.CreatedItemSlot) && + !props.HasItemEquipped && props.ItemAnimationState < 0) return; Logger.Log($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)} puts away the item in " + @@ -924,18 +925,32 @@ public void RemoveUsedItemFromHand(NpcContainer npcContainer) } /// - /// Only the used item's mesh in its slot goes (an ItemRemove/ItemDestroy animation event); the item state - /// stays. Returns whether a mesh was destroyed. + /// Only the used item's mesh in its slot goes (an ItemRemove/ItemDestroy animation event), and an item a model + /// script created (the smith's hammer); the item state stays. Returns whether a mesh was destroyed. /// public bool DestroyUsedItemMesh(NpcContainer npcContainer) { var props = npcContainer?.Props; - if (props == null || string.IsNullOrEmpty(props.UsedItemSlot)) + if (props == null) + return false; + + var destroyed = DestroySlotMeshes(npcContainer, props.UsedItemSlot); + props.UsedItemSlot = string.Empty; + + destroyed |= DestroySlotMeshes(npcContainer, props.CreatedItemSlot); + props.CreatedItemSlot = string.Empty; + + return destroyed; + } + + private static bool DestroySlotMeshes(NpcContainer npcContainer, string slot) + { + if (string.IsNullOrEmpty(slot)) return false; var destroyed = false; var slotGo = npcContainer.PrefabProps?.Bip01 != null - ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(props.UsedItemSlot) + ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot) : null; if (slotGo != null) { @@ -946,10 +961,40 @@ public bool DestroyUsedItemMesh(NpcContainer npcContainer) } } - props.UsedItemSlot = string.Empty; return destroyed; } + /// + /// DEF_CREATE_ITEM (animation event ItemCreate): the model script puts a new item into a slot, e.g. + /// T_BSANVIL_S0_2_S1 the hammer ITMW_1H_MACE_L_04 into the smith's right hand (the hot blade comes from the + /// mob, ItemInsert). Only its mesh - the NPC's inventory doesn't change. Gone with DEF_DESTROY_ITEM. + /// + public void CreateItem(NpcContainer npcContainer, string slot, string itemName) + { + var npcName = npcContainer.Instance.GetName(NpcNameSlot.Slot0); + var item = itemName.IsNullOrEmpty() ? null : _vm.GetSymbolByName(itemName); + if (item == null) + { + Logger.LogWarning($"[NpcService] {npcName}: DEF_CREATE_ITEM item '{itemName}' isn't in the scripts - " + + "nothing created.", LogCat.Npc); + return; + } + + var slotGo = npcContainer.PrefabProps?.Bip01 != null + ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot) + : null; + if (slotGo == null) + { + Logger.LogWarning($"[NpcService] {npcName}: no slot '{slot}' for DEF_CREATE_ITEM '{itemName}'.", + LogCat.Npc); + return; + } + + DestroySlotMeshes(npcContainer, npcContainer.Props.CreatedItemSlot); + _vobService.CreateItemMesh(item.Index, slotGo); + npcContainer.Props.CreatedItemSlot = slot; + } + public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = "") { if (slot2.Any()) @@ -958,7 +1003,10 @@ public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = " } // The slot holds one used item, like the engine's state item - a new one replaces, never adds. - if (DestroyUsedItemMesh(npcContainer)) + var props = npcContainer.Props; + var isReplaced = DestroySlotMeshes(npcContainer, props.UsedItemSlot); + props.UsedItemSlot = string.Empty; + if (isReplaced) Logger.LogWarning($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)}: an item was " + $"still in the hand - replaced by the new one in '{slot1}'", LogCat.Npc); From 9ec181b7b9c110a17fd8caede425fc779ff8b17b Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:47:22 +0000 Subject: [PATCH 2/8] fix: the drummer keeps both drumsticks and a drawn weapon survives the put-away - only the item meshes we created go NpcProperties remembered the used and the created item by slot name, and putting them away destroyed every child of that slot. T_PAUKE_STAND_2_S0 creates a drumstick in each hand, so the second ItemCreate deleted the first one, and a weapon drawn into ZS_RIGHTHAND would have gone together with the hammer. The NPC now keeps the item GameObjects themselves (UsedItemGo, CreatedItemGos) and destroys exactly those. A slot holds one item like the engine's slots: a new ItemCreate replaces only the used/created mesh in the same slot. InsertItem still replaces the used item, ItemDestroy/ItemRemove and RemoveUsedItemFromHand still remove the used and the created items. Co-Authored-By: Claude Opus 5.5 --- .../Adapters/Animations/AnimationSystem.cs | 2 +- .../Adapters/Properties/NpcProperties.cs | 11 ++- .../Scripts/Services/Npc/NpcService.cs | 85 ++++++++++++------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs index 1686387cb..9e81be7d7 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs @@ -886,7 +886,7 @@ private void ApplyEventTags(AnimationTrackInstance trackInstance) break; case EventType.ItemDestroy: case EventType.ItemRemove: - // The slot is cleared too - a stale one could later delete a drawn weapon. + // Only the item meshes we put in go - a drawn weapon in the same hand stays. _npcService.DestroyUsedItemMesh(NpcData); break; case EventType.TorchInventory: diff --git a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs index 077c3afaf..759f84a74 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs @@ -4,6 +4,7 @@ using Gothic.Core.Models.Npc; using Gothic.Core.Models.Vm; using Gothic.Core.Models.Vob.WayNet; +using UnityEngine; using ZenKit.Daedalus; namespace Gothic.Core.Adapters.Properties @@ -26,10 +27,12 @@ public class NpcProperties // NPC items/talents/... public List EquippedItems = new(); - public string UsedItemSlot; - // The slot of an item a model script created (DEF_CREATE_ITEM, e.g. the smith's hammer at the anvil). It goes - // together with the used item (DEF_DESTROY_ITEM, the item put away). - public string CreatedItemSlot; + // The mesh of the used item in its slot (DEF_INSERT_ITEM: the food, the mob's item). Only this object goes when + // the item is put away - never the rest of the slot, a drawn weapon in the same hand stays. + public GameObject UsedItemGo; + // Meshes a model script created (DEF_CREATE_ITEM, e.g. the smith's hammer at the anvil, both drumsticks of + // T_PAUKE). They go together with the used item (DEF_DESTROY_ITEM, the item put away). + public List CreatedItemGos = new(); public bool HasItemEquipped; public int CurrentItem = -1; // We need to start with an "invalid" value as >0< is an allowed state value like in >t_Potion_Stand_2_S0< diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 83a1ede19..c91076119 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -906,12 +906,15 @@ public void RemoveUsedItemFromHand(NpcContainer npcContainer) var props = npcContainer?.Props; if (props == null) return; - if (string.IsNullOrEmpty(props.UsedItemSlot) && string.IsNullOrEmpty(props.CreatedItemSlot) && - !props.HasItemEquipped && props.ItemAnimationState < 0) + if (props.UsedItemGo == null && props.CreatedItemGos.Count == 0 && !props.HasItemEquipped && + props.ItemAnimationState < 0) return; + var slotName = props.UsedItemGo != null && props.UsedItemGo.transform.parent != null + ? props.UsedItemGo.transform.parent.name + : string.Empty; Logger.Log($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)} puts away the item in " + - $"'{props.UsedItemSlot}' (state {props.ItemAnimationState})", LogCat.Npc); + $"'{slotName}' (state {props.ItemAnimationState})", LogCat.Npc); DestroyUsedItemMesh(npcContainer); props.ItemAnimationState = -1; @@ -925,8 +928,9 @@ public void RemoveUsedItemFromHand(NpcContainer npcContainer) } /// - /// Only the used item's mesh in its slot goes (an ItemRemove/ItemDestroy animation event), and an item a model - /// script created (the smith's hammer); the item state stays. Returns whether a mesh was destroyed. + /// Only the used item's mesh goes (an ItemRemove/ItemDestroy animation event), and the items a model script + /// created (the smith's hammer); the item state stays. Only these objects - a drawn weapon in the same slot + /// stays. Returns whether a mesh was destroyed. /// public bool DestroyUsedItemMesh(NpcContainer npcContainer) { @@ -934,40 +938,58 @@ public bool DestroyUsedItemMesh(NpcContainer npcContainer) if (props == null) return false; - var destroyed = DestroySlotMeshes(npcContainer, props.UsedItemSlot); - props.UsedItemSlot = string.Empty; + var destroyed = DestroyItemGo(props.UsedItemGo); + props.UsedItemGo = null; - destroyed |= DestroySlotMeshes(npcContainer, props.CreatedItemSlot); - props.CreatedItemSlot = string.Empty; + foreach (var createdGo in props.CreatedItemGos) + destroyed |= DestroyItemGo(createdGo); + props.CreatedItemGos.Clear(); return destroyed; } - private static bool DestroySlotMeshes(NpcContainer npcContainer, string slot) + /// + /// A slot holds one item, like the engine's slots: the used and created item meshes in it go. Other children + /// of the slot (a drawn weapon) stay. Returns whether a mesh was destroyed. + /// + private static bool DestroyItemsInSlot(NpcContainer npcContainer, GameObject slotGo) { - if (string.IsNullOrEmpty(slot)) - return false; - + var props = npcContainer.Props; var destroyed = false; - var slotGo = npcContainer.PrefabProps?.Bip01 != null - ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot) - : null; - if (slotGo != null) + if (props.UsedItemGo != null && props.UsedItemGo.transform.parent == slotGo.transform) { - for (var i = slotGo.transform.childCount - 1; i >= 0; i--) - { - Object.Destroy(slotGo.transform.GetChild(i).gameObject); - destroyed = true; - } + destroyed = DestroyItemGo(props.UsedItemGo); + props.UsedItemGo = null; + } + + for (var i = props.CreatedItemGos.Count - 1; i >= 0; i--) + { + var createdGo = props.CreatedItemGos[i]; + // Items in other slots stay. One already gone (the NPC's body was destroyed) is forgotten as well. + if (createdGo != null && createdGo.transform.parent != slotGo.transform) + continue; + + destroyed |= DestroyItemGo(createdGo); + props.CreatedItemGos.RemoveAt(i); } return destroyed; } + private static bool DestroyItemGo(GameObject itemGo) + { + if (itemGo == null) + return false; + + Object.Destroy(itemGo); + return true; + } + /// /// DEF_CREATE_ITEM (animation event ItemCreate): the model script puts a new item into a slot, e.g. /// T_BSANVIL_S0_2_S1 the hammer ITMW_1H_MACE_L_04 into the smith's right hand (the hot blade comes from the - /// mob, ItemInsert). Only its mesh - the NPC's inventory doesn't change. Gone with DEF_DESTROY_ITEM. + /// mob, ItemInsert), T_PAUKE_STAND_2_S0 a drumstick into each hand. Only its mesh - the NPC's inventory doesn't + /// change. Gone with DEF_DESTROY_ITEM. /// public void CreateItem(NpcContainer npcContainer, string slot, string itemName) { @@ -990,9 +1012,10 @@ public void CreateItem(NpcContainer npcContainer, string slot, string itemName) return; } - DestroySlotMeshes(npcContainer, npcContainer.Props.CreatedItemSlot); - _vobService.CreateItemMesh(item.Index, slotGo); - npcContainer.Props.CreatedItemSlot = slot; + DestroyItemsInSlot(npcContainer, slotGo); + var itemGo = _vobService.CreateItemMesh(item.Index, slotGo); + if (itemGo != null) + npcContainer.Props.CreatedItemGos.Add(itemGo); } public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = "") @@ -1002,19 +1025,17 @@ public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = " throw new Exception("Slot 2 is set but not yet handled by InsertItem as AnimationEvent."); } - // The slot holds one used item, like the engine's state item - a new one replaces, never adds. + // The NPC holds one used item, like the engine's state item - a new one replaces, never adds. var props = npcContainer.Props; - var isReplaced = DestroySlotMeshes(npcContainer, props.UsedItemSlot); - props.UsedItemSlot = string.Empty; + var isReplaced = DestroyItemGo(props.UsedItemGo); + props.UsedItemGo = null; if (isReplaced) Logger.LogWarning($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)}: an item was " + $"still in the hand - replaced by the new one in '{slot1}'", LogCat.Npc); var slotGo = npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot1); - _vobService.CreateItemMesh(npcContainer.Props.CurrentItem, slotGo); - - npcContainer.Props.UsedItemSlot = slot1; + props.UsedItemGo = _vobService.CreateItemMesh(props.CurrentItem, slotGo); } } } From 62c55c47adc120878bedf1a2dc4a1b98142d6e05 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:48:07 +0000 Subject: [PATCH 3/8] fix: a model script item without a mesh or a non-item name is skipped with a warning instead of an error CreateItem looked the DEF_CREATE_ITEM item up as a raw VM symbol, so a symbol that isn't an item instance reached TryGetItemData and the mesh builder with null (NullReferenceException), and an item whose mesh wasn't built was remembered anyway. It now goes through VobService.CreateItemMesh(string), which resolves the item via VmCacheService, warns for a non-item and returns the built GameObject (both overloads guard a null item). Only a built mesh is remembered. The slot lookup is one FindSlot helper; InsertItem warns about a missing slot instead of putting the mesh at the scene root. Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Services/Npc/NpcService.cs | 41 ++++++++++++------- .../Scripts/Services/Vobs/VobService.cs | 22 +++++++--- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index c91076119..463ed0502 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -994,17 +994,7 @@ private static bool DestroyItemGo(GameObject itemGo) public void CreateItem(NpcContainer npcContainer, string slot, string itemName) { var npcName = npcContainer.Instance.GetName(NpcNameSlot.Slot0); - var item = itemName.IsNullOrEmpty() ? null : _vm.GetSymbolByName(itemName); - if (item == null) - { - Logger.LogWarning($"[NpcService] {npcName}: DEF_CREATE_ITEM item '{itemName}' isn't in the scripts - " + - "nothing created.", LogCat.Npc); - return; - } - - var slotGo = npcContainer.PrefabProps?.Bip01 != null - ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot) - : null; + var slotGo = FindSlot(npcContainer, slot); if (slotGo == null) { Logger.LogWarning($"[NpcService] {npcName}: no slot '{slot}' for DEF_CREATE_ITEM '{itemName}'.", @@ -1013,9 +1003,15 @@ public void CreateItem(NpcContainer npcContainer, string slot, string itemName) } DestroyItemsInSlot(npcContainer, slotGo); - var itemGo = _vobService.CreateItemMesh(item.Index, slotGo); - if (itemGo != null) - npcContainer.Props.CreatedItemGos.Add(itemGo); + var itemGo = _vobService.CreateItemMesh(itemName, slotGo); + if (itemGo == null) + { + Logger.LogWarning($"[NpcService] {npcName}: no mesh for DEF_CREATE_ITEM '{itemName}' in '{slot}' - " + + "nothing created.", LogCat.Npc); + return; + } + + npcContainer.Props.CreatedItemGos.Add(itemGo); } public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = "") @@ -1033,9 +1029,24 @@ public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = " Logger.LogWarning($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)}: an item was " + $"still in the hand - replaced by the new one in '{slot1}'", LogCat.Npc); - var slotGo = npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(slot1); + var slotGo = FindSlot(npcContainer, slot1); + if (slotGo == null) + { + Logger.LogWarning($"[NpcService] {npcContainer.Instance.GetName(NpcNameSlot.Slot0)}: no slot " + + $"'{slot1}' for DEF_INSERT_ITEM - nothing inserted.", LogCat.Npc); + return; + } props.UsedItemGo = _vobService.CreateItemMesh(props.CurrentItem, slotGo); } + + private static GameObject FindSlot(NpcContainer npcContainer, string slot) + { + var bip01 = npcContainer.PrefabProps?.Bip01; + if (bip01 == null || string.IsNullOrEmpty(slot)) + return null; + + return bip01.gameObject.FindChildRecursively(slot); + } } } diff --git a/Assets/Gothic-Core/Scripts/Services/Vobs/VobService.cs b/Assets/Gothic-Core/Scripts/Services/Vobs/VobService.cs index bcff9ee93..899e9e01b 100644 --- a/Assets/Gothic-Core/Scripts/Services/Vobs/VobService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Vobs/VobService.cs @@ -256,24 +256,36 @@ public GameObject CreateItemMesh(int itemId, GameObject parentGo) return null; // no item } var item = _vmCacheService.TryGetItemData(itemId); + if (item == null) + { + Logger.LogWarning($"Symbol {itemId} is no item instance - no item mesh created.", LogCat.Vob); + return null; + } return _initializerDomain.CreateItemMesh(item, parentGo, default); } /// /// Create item with mesh only. No special handling like grabbing etc. - /// e.g. used for NPCs drinking beer mesh in their hand. + /// e.g. used for an item a model script puts into an NPC's hand (DEF_CREATE_ITEM). Returns null when no mesh + /// was built. /// - public void CreateItemMesh(string itemName, GameObject parentGo) + public GameObject CreateItemMesh(string itemName, GameObject parentGo) { - if (itemName == "") + if (string.IsNullOrEmpty(itemName)) { - return; + return null; } var item = _vmCacheService.TryGetItemData(itemName); + if (item == null) + { + Logger.LogWarning($"'{itemName}' is no item instance in the scripts - no item mesh created.", + LogCat.Vob); + return null; + } - _initializerDomain.CreateItemMesh(item, parentGo, default); + return _initializerDomain.CreateItemMesh(item, parentGo, default); } /// From 6afffb0f1c5ef7bb50c8f158c0b4a4f93903c024 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 21:49:00 +0000 Subject: [PATCH 4/8] fix: the smith's blade glows after the forge fire and cooks hold the fried meat - the model script's DEF_EXCHANGE_ITEM swaps the item in the hand ItemExchange fell through to "not yet supported": T_BSFIRE_S0_2_S1 (ZS_LEFTHAND ITMISWORDRAWHOT) left the cold blade in the smith's hand, T_PAN_S1_2_S0 and T_STOVE_S0_2_STAND (ZS_LEFTHAND ITFOMUTTON) never showed the fried meat. Like OpenGothic's tickTimedEvt, the item meshes in that slot go; if the slot is empty (its cooking fallback) the used item goes instead. Then the new item's mesh goes into the slot and is remembered as created, so ItemDestroy or putting the item away removes it. CreateItem and ExchangeItem share PutItemIntoSlot. The inventory doesn't change (OpenGothic adds the item to it). Co-Authored-By: Claude Opus 5.5 --- .../Adapters/Animations/AnimationSystem.cs | 3 ++ .../Scripts/Services/Npc/NpcService.cs | 34 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs index 9e81be7d7..e4aa201d6 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs @@ -884,6 +884,9 @@ private void ApplyEventTags(AnimationTrackInstance trackInstance) case EventType.ItemCreate: _npcService.CreateItem(NpcData, eventTag.Slots.Item1, eventTag.Item); break; + case EventType.ItemExchange: + _npcService.ExchangeItem(NpcData, eventTag.Slots.Item1, eventTag.Item); + break; case EventType.ItemDestroy: case EventType.ItemRemove: // Only the item meshes we put in go - a drawn weapon in the same hand stays. diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 463ed0502..c0dbb9e86 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -992,26 +992,52 @@ private static bool DestroyItemGo(GameObject itemGo) /// change. Gone with DEF_DESTROY_ITEM. /// public void CreateItem(NpcContainer npcContainer, string slot, string itemName) + { + PutItemIntoSlot(npcContainer, slot, itemName, false); + } + + /// + /// DEF_EXCHANGE_ITEM (animation event ItemExchange): the item in the slot becomes the given one, e.g. + /// T_BSFIRE_S0_2_S1 turns the smith's blade into the glowing ITMISWORDRAWHOT, T_PAN_S1_2_S0 puts the fried + /// ITFOMUTTON into the left hand. Like OpenGothic: the slot's item goes; is the slot empty (its cooking + /// fallback), the used item goes instead. The new item counts as created, so DEF_DESTROY_ITEM or putting it + /// away removes it. Only its mesh, like DEF_CREATE_ITEM: the NPC's inventory doesn't change. + /// + public void ExchangeItem(NpcContainer npcContainer, string slot, string itemName) + { + PutItemIntoSlot(npcContainer, slot, itemName, true); + } + + private void PutItemIntoSlot(NpcContainer npcContainer, string slot, string itemName, bool isExchange) { var npcName = npcContainer.Instance.GetName(NpcNameSlot.Slot0); + var eventName = isExchange ? "DEF_EXCHANGE_ITEM" : "DEF_CREATE_ITEM"; var slotGo = FindSlot(npcContainer, slot); if (slotGo == null) { - Logger.LogWarning($"[NpcService] {npcName}: no slot '{slot}' for DEF_CREATE_ITEM '{itemName}'.", + Logger.LogWarning($"[NpcService] {npcName}: no slot '{slot}' for {eventName} '{itemName}'.", LogCat.Npc); return; } - DestroyItemsInSlot(npcContainer, slotGo); + var props = npcContainer.Props; + var isSlotCleared = DestroyItemsInSlot(npcContainer, slotGo); + if (isExchange && !isSlotCleared && DestroyItemGo(props.UsedItemGo)) + { + props.UsedItemGo = null; + Logger.Log($"[NpcService] {npcName}: {eventName} '{itemName}' - nothing in '{slot}', the used item " + + "goes instead.", LogCat.Npc); + } + var itemGo = _vobService.CreateItemMesh(itemName, slotGo); if (itemGo == null) { - Logger.LogWarning($"[NpcService] {npcName}: no mesh for DEF_CREATE_ITEM '{itemName}' in '{slot}' - " + + Logger.LogWarning($"[NpcService] {npcName}: no mesh for {eventName} '{itemName}' in '{slot}' - " + "nothing created.", LogCat.Npc); return; } - npcContainer.Props.CreatedItemGos.Add(itemGo); + props.CreatedItemGos.Add(itemGo); } public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = "") From 51d7edf9e28a98fb128a8263f4681e0ad2f97a71 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 22:46:57 +0000 Subject: [PATCH 5/8] fix: an item exchange to nothing just clears the slot - no 'no mesh' warning Co-Authored-By: Claude Sonnet 5.5 --- Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index c0dbb9e86..90b8e54c8 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -1029,6 +1029,13 @@ private void PutItemIntoSlot(NpcContainer npcContainer, string slot, string item "goes instead.", LogCat.Npc); } + // An exchange to nothing (empty item name) is a plain clear. + if (isExchange && string.IsNullOrEmpty(itemName)) + { + Logger.Log($"[NpcService] {npcName}: {eventName} to nothing - '{slot}' cleared.", LogCat.Npc); + return; + } + var itemGo = _vobService.CreateItemMesh(itemName, slotGo); if (itemGo == null) { From abee13f8245b5c6130d9e7cdf44ee7c36a39cc34 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 23:11:15 +0000 Subject: [PATCH 6/8] fix: items a model script puts into an NPC's hand are in his inventory too, like the engine - Harad's hot blade can be bought or stolen Co-Authored-By: Claude Opus 5.5 --- .../Adapters/Animations/AnimationSystem.cs | 8 +- .../Adapters/Properties/NpcProperties.cs | 8 +- .../Scripts/Services/Npc/NpcService.cs | 118 +++++++++++++++--- 3 files changed, 111 insertions(+), 23 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs index e4aa201d6..22dcb9654 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs @@ -888,10 +888,14 @@ private void ApplyEventTags(AnimationTrackInstance trackInstance) _npcService.ExchangeItem(NpcData, eventTag.Slots.Item1, eventTag.Item); break; case EventType.ItemDestroy: - case EventType.ItemRemove: - // Only the item meshes we put in go - a drawn weapon in the same hand stays. + // Only the item meshes we put in go - a drawn weapon in the same hand stays. The items the + // model script created leave the NPC's inventory too. _npcService.DestroyUsedItemMesh(NpcData); break; + case EventType.ItemRemove: + // Like ItemDestroy, but the created items stay in the NPC's inventory. + _npcService.DestroyUsedItemMesh(NpcData, isInventoryKept: true); + break; case EventType.TorchInventory: // TODO - I assume this means: if torch is in inventory, then put it out. But not really sure. Need a NPC with real usage of it to predict right. break; diff --git a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs index 759f84a74..7c6fe4d53 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Properties/NpcProperties.cs @@ -30,9 +30,11 @@ public class NpcProperties // The mesh of the used item in its slot (DEF_INSERT_ITEM: the food, the mob's item). Only this object goes when // the item is put away - never the rest of the slot, a drawn weapon in the same hand stays. public GameObject UsedItemGo; - // Meshes a model script created (DEF_CREATE_ITEM, e.g. the smith's hammer at the anvil, both drumsticks of - // T_PAUKE). They go together with the used item (DEF_DESTROY_ITEM, the item put away). - public List CreatedItemGos = new(); + // Items a model script created (DEF_CREATE_ITEM/DEF_EXCHANGE_ITEM, e.g. the smith's hammer at the anvil, both + // drumsticks of T_PAUKE): the mesh and the item added to the NPC's inventory for it (-1: none was added). + // DEF_DESTROY_ITEM or putting them away removes both, DEF_REMOVE_ITEM only the mesh. Not saved: an item added + // mid-animation stays in the saved inventory and the hand is empty after loading, like the original. + public List<(GameObject Go, int ItemIndex)> CreatedItems = new(); public bool HasItemEquipped; public int CurrentItem = -1; // We need to start with an "invalid" value as >0< is an allowed state value like in >t_Potion_Stand_2_S0< diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 90b8e54c8..7dcf3ccf2 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -900,13 +900,15 @@ private void EventNpcMeshCullingChanged(NpcContainer npcContainer, NpcLoader npc /// An item the NPC holds from AI_UseItemToState (food, a torch, ...) is put away: its mesh in the slot goes, /// the item state resets. Like the engine's clearState/AI_StandUp - an NPC interrupted while eating (aggro) /// kept the apple, and the next AI_UseItemToState put a second one into its hand. + /// The items a model script created go like with DEF_DESTROY_ITEM, out of the inventory too - a smith + /// interrupted at the anvil would collect a hammer each time. /// public void RemoveUsedItemFromHand(NpcContainer npcContainer) { var props = npcContainer?.Props; if (props == null) return; - if (props.UsedItemGo == null && props.CreatedItemGos.Count == 0 && !props.HasItemEquipped && + if (props.UsedItemGo == null && props.CreatedItems.Count == 0 && !props.HasItemEquipped && props.ItemAnimationState < 0) return; @@ -930,9 +932,11 @@ public void RemoveUsedItemFromHand(NpcContainer npcContainer) /// /// Only the used item's mesh goes (an ItemRemove/ItemDestroy animation event), and the items a model script /// created (the smith's hammer); the item state stays. Only these objects - a drawn weapon in the same slot - /// stays. Returns whether a mesh was destroyed. + /// stays. Like the engine's clearSlot, the created items also leave the NPC's inventory (DEF_DESTROY_ITEM, + /// put away) unless isInventoryKept (DEF_REMOVE_ITEM). The used item's inventory never changes here. + /// Returns whether a mesh was destroyed. /// - public bool DestroyUsedItemMesh(NpcContainer npcContainer) + public bool DestroyUsedItemMesh(NpcContainer npcContainer, bool isInventoryKept = false) { var props = npcContainer?.Props; if (props == null) @@ -941,18 +945,24 @@ public bool DestroyUsedItemMesh(NpcContainer npcContainer) var destroyed = DestroyItemGo(props.UsedItemGo); props.UsedItemGo = null; - foreach (var createdGo in props.CreatedItemGos) - destroyed |= DestroyItemGo(createdGo); - props.CreatedItemGos.Clear(); + foreach (var createdItem in props.CreatedItems) + { + destroyed |= DestroyItemGo(createdItem.Go); + if (!isInventoryKept) + RemoveCreatedItemFromInventory(npcContainer, createdItem.ItemIndex); + } + props.CreatedItems.Clear(); return destroyed; } /// /// A slot holds one item, like the engine's slots: the used and created item meshes in it go. Other children - /// of the slot (a drawn weapon) stay. Returns whether a mesh was destroyed. + /// of the slot (a drawn weapon) stay. A created item also leaves the inventory (the engine's clearSlot with + /// remove) - an item replaced by the next DEF_CREATE_ITEM too, else a replayed animation piles them up. + /// Returns whether a mesh was destroyed. /// - private static bool DestroyItemsInSlot(NpcContainer npcContainer, GameObject slotGo) + private bool DestroyItemsInSlot(NpcContainer npcContainer, GameObject slotGo) { var props = npcContainer.Props; var destroyed = false; @@ -962,20 +972,88 @@ private static bool DestroyItemsInSlot(NpcContainer npcContainer, GameObject slo props.UsedItemGo = null; } - for (var i = props.CreatedItemGos.Count - 1; i >= 0; i--) + for (var i = props.CreatedItems.Count - 1; i >= 0; i--) { - var createdGo = props.CreatedItemGos[i]; + var createdItem = props.CreatedItems[i]; // Items in other slots stay. One already gone (the NPC's body was destroyed) is forgotten as well. - if (createdGo != null && createdGo.transform.parent != slotGo.transform) + if (createdItem.Go != null && createdItem.Go.transform.parent != slotGo.transform) continue; - destroyed |= DestroyItemGo(createdGo); - props.CreatedItemGos.RemoveAt(i); + destroyed |= DestroyItemGo(createdItem.Go); + RemoveCreatedItemFromInventory(npcContainer, createdItem.ItemIndex); + props.CreatedItems.RemoveAt(i); } return destroyed; } + /// + /// Like the engine (OpenGothic Npc::tickTimedEvt: addItem, then putToSlot), an item a model script puts into + /// the hand is in the NPC's inventory while he holds it - the trade screen and pickpocketing see it (buying + /// Harad's hot blade, an original exploit). Through the NPC's packed inventory (NpcInventoryService), like + /// CreateInvItems. A save taken mid-animation keeps the item in the inventory, like the original. + /// Returns the added item's index, -1 when nothing was added. + /// + private int AddCreatedItemToInventory(NpcContainer npcContainer, string itemName, string eventName) + { + var npc = npcContainer.Instance; + var npcName = npc.GetName(NpcNameSlot.Slot0); + var item = _vmCacheService.TryGetItemData(itemName); + if (item == null) + { + Logger.LogWarning($"[NpcService] {npcName}: {eventName} '{itemName}' is no item - not added to the " + + "inventory.", LogCat.Npc); + return -1; + } + + var countBefore = _npcInventoryService.ExtNpcHasItems(npc, item.Index); + _npcInventoryService.ExtCreateInvItems(npc, item.Index, 1); + var countAfter = _npcInventoryService.ExtNpcHasItems(npc, item.Index); + if (countAfter != countBefore + 1) + { + Logger.LogWarning($"[NpcService] {npcName}: {eventName} '{itemName}' wasn't added to the inventory " + + $"({countBefore} -> {countAfter}) - only its mesh is in the hand.", LogCat.Npc); + return -1; + } + + Logger.LogEditor($"[NpcService] {npcName}: {eventName} '{itemName}' added to the inventory " + + $"({countAfter} now).", LogCat.Npc); + return item.Index; + } + + /// + /// A created item leaves the hand for good: the one added for it leaves the inventory, once. If it is no longer + /// there (sold or stolen while in the hand), nothing goes - the original's "infinite steel" from Harad stays. + /// + private void RemoveCreatedItemFromInventory(NpcContainer npcContainer, int itemIndex) + { + if (itemIndex < 0) + return; + + var npc = npcContainer.Instance; + var npcName = npc.GetName(NpcNameSlot.Slot0); + var itemName = _vm.GetSymbolByIndex(itemIndex)?.Name ?? itemIndex.ToString(); + var countBefore = _npcInventoryService.ExtNpcHasItems(npc, itemIndex); + if (countBefore <= 0) + { + Logger.LogWarning($"[NpcService] {npcName}: the created '{itemName}' isn't in the inventory any " + + "more (sold or stolen from the hand) - nothing removed.", LogCat.Npc); + return; + } + + _npcInventoryService.ExtRemoveInvItems(npc, itemIndex, 1); + var countAfter = _npcInventoryService.ExtNpcHasItems(npc, itemIndex); + if (countAfter != countBefore - 1) + { + Logger.LogWarning($"[NpcService] {npcName}: the created '{itemName}' wasn't removed from the " + + $"inventory ({countBefore} -> {countAfter}).", LogCat.Npc); + return; + } + + Logger.LogEditor($"[NpcService] {npcName}: the created '{itemName}' removed from the inventory " + + $"({countAfter} left).", LogCat.Npc); + } + private static bool DestroyItemGo(GameObject itemGo) { if (itemGo == null) @@ -988,8 +1066,9 @@ private static bool DestroyItemGo(GameObject itemGo) /// /// DEF_CREATE_ITEM (animation event ItemCreate): the model script puts a new item into a slot, e.g. /// T_BSANVIL_S0_2_S1 the hammer ITMW_1H_MACE_L_04 into the smith's right hand (the hot blade comes from the - /// mob, ItemInsert), T_PAUKE_STAND_2_S0 a drumstick into each hand. Only its mesh - the NPC's inventory doesn't - /// change. Gone with DEF_DESTROY_ITEM. + /// mob, ItemInsert), T_PAUKE_STAND_2_S0 a drumstick into each hand. Like the engine, the item is added to the + /// NPC's inventory (+1) while in the hand. DEF_DESTROY_ITEM or putting it away removes the mesh and the item, + /// DEF_REMOVE_ITEM only the mesh. /// public void CreateItem(NpcContainer npcContainer, string slot, string itemName) { @@ -1000,8 +1079,9 @@ public void CreateItem(NpcContainer npcContainer, string slot, string itemName) /// DEF_EXCHANGE_ITEM (animation event ItemExchange): the item in the slot becomes the given one, e.g. /// T_BSFIRE_S0_2_S1 turns the smith's blade into the glowing ITMISWORDRAWHOT, T_PAN_S1_2_S0 puts the fried /// ITFOMUTTON into the left hand. Like OpenGothic: the slot's item goes; is the slot empty (its cooking - /// fallback), the used item goes instead. The new item counts as created, so DEF_DESTROY_ITEM or putting it - /// away removes it. Only its mesh, like DEF_CREATE_ITEM: the NPC's inventory doesn't change. + /// fallback), the used item's mesh goes instead. A created item in the slot leaves the inventory (the + /// engine's clearSlot with remove; the used item's inventory doesn't change), the new item is added (+1) and + /// counts as created, like DEF_CREATE_ITEM. /// public void ExchangeItem(NpcContainer npcContainer, string slot, string itemName) { @@ -1044,7 +1124,9 @@ private void PutItemIntoSlot(NpcContainer npcContainer, string slot, string item return; } - props.CreatedItemGos.Add(itemGo); + // Only an item with a mesh is added: the hand shows what the trade screen and pickpocketing offer. + var itemIndex = AddCreatedItemToInventory(npcContainer, itemName, eventName); + props.CreatedItems.Add((itemGo, itemIndex)); } public void InsertItem(NpcContainer npcContainer, string slot1, string slot2 = "") From 0d4cf0a799b2421eff52dfcf95ad8f0e742f63bb Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 23:33:59 +0000 Subject: [PATCH 7/8] docs: ADR - items from working-place animations are in the NPC's hands and inventory Co-Authored-By: Claude Sonnet 5.5 --- ...6-11-25-model-script-items-in-inventory.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Docs/adr/2026-11-25-model-script-items-in-inventory.md diff --git a/Docs/adr/2026-11-25-model-script-items-in-inventory.md b/Docs/adr/2026-11-25-model-script-items-in-inventory.md new file mode 100644 index 000000000..678d8d18a --- /dev/null +++ b/Docs/adr/2026-11-25-model-script-items-in-inventory.md @@ -0,0 +1,22 @@ +# ADR: Items from working-place animations are in the NPC's hands and inventory + +- Date: 2026-11-25 +- Technical details: Docs/npc-ai.md + +## Context + +Gothic's animations of working places put items into the NPC's hands: the smith's hammer at the anvil, the glowing blade from the forge, a drummer's sticks, a cook's pan or meat. +We showed none of them. +The original also puts these items into the NPC's inventory for as long as he holds them. + +## Decisions + +1. **The items appear in the hands like in the original, and go with the end of the animation or when the NPC is interrupted.** The workplaces look alive again, and nothing stays in a hand after the work is done. +2. **While held, the items are in the NPC's inventory like in the engine, so they can be bought or stolen.** This keeps the original's "infinite steel" trick at Harad on purpose. Not chosen: only showing the item, which hides what the original lets players do. +3. **Nothing extra is saved.** An item saved while held stays in the NPC's inventory, like the original. + +## Consequences + +- Harad's hot blade can be bought or pickpocketed while he holds it. +- A save and load while an item is held gives the NPC one more item. +- NPC fights that look at the inventory may see the hammer, like in the original. From 9e0a61f28f4fb4202b3323642a6050036762f801 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Wed, 25 Nov 2026 23:34:03 +0000 Subject: [PATCH 8/8] fix: a created item that was sold or stolen from the hand is an expected state - logged as info, not warning Co-Authored-By: Claude Sonnet 5.5 --- Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs index 7dcf3ccf2..6c354d5da 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -1036,8 +1036,8 @@ private void RemoveCreatedItemFromInventory(NpcContainer npcContainer, int itemI var countBefore = _npcInventoryService.ExtNpcHasItems(npc, itemIndex); if (countBefore <= 0) { - Logger.LogWarning($"[NpcService] {npcName}: the created '{itemName}' isn't in the inventory any " + - "more (sold or stolen from the hand) - nothing removed.", LogCat.Npc); + Logger.Log($"[NpcService] {npcName}: the created '{itemName}' isn't in the inventory any " + + "more (sold or stolen from the hand) - nothing removed.", LogCat.Npc); return; }