diff --git a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs index ad2b69016..22dcb9654 100644 --- a/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs +++ b/Assets/Gothic-Core/Scripts/Adapters/Animations/AnimationSystem.cs @@ -881,11 +881,21 @@ 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.ItemExchange: + _npcService.ExchangeItem(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. + // 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 31d2f4a7c..7c6fe4d53 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,7 +27,14 @@ public class NpcProperties // NPC items/talents/... public List EquippedItems = new(); - public string UsedItemSlot; + // 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; + // 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 78a44b9ea..6c354d5da 100644 --- a/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Npc/NpcService.cs @@ -900,17 +900,23 @@ 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 (string.IsNullOrEmpty(props.UsedItemSlot) && !props.HasItemEquipped && props.ItemAnimationState < 0) + if (props.UsedItemGo == null && props.CreatedItems.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; @@ -924,32 +930,205 @@ 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 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. 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 || string.IsNullOrEmpty(props.UsedItemSlot)) + if (props == null) return false; + var destroyed = DestroyItemGo(props.UsedItemGo); + props.UsedItemGo = null; + + 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. 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 bool DestroyItemsInSlot(NpcContainer npcContainer, GameObject slotGo) + { + var props = npcContainer.Props; var destroyed = false; - var slotGo = npcContainer.PrefabProps?.Bip01 != null - ? npcContainer.PrefabProps.Bip01.gameObject.FindChildRecursively(props.UsedItemSlot) - : 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.CreatedItems.Count - 1; i >= 0; 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 (createdItem.Go != null && createdItem.Go.transform.parent != slotGo.transform) + continue; + + destroyed |= DestroyItemGo(createdItem.Go); + RemoveCreatedItemFromInventory(npcContainer, createdItem.ItemIndex); + props.CreatedItems.RemoveAt(i); } - props.UsedItemSlot = string.Empty; 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.Log($"[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) + 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), 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) + { + 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'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) + { + 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 {eventName} '{itemName}'.", + LogCat.Npc); + return; + } + + 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); + } + + // 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) + { + Logger.LogWarning($"[NpcService] {npcName}: no mesh for {eventName} '{itemName}' in '{slot}' - " + + "nothing created.", LogCat.Npc); + return; + } + + // 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 = "") { if (slot2.Any()) @@ -957,16 +1136,32 @@ 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. - if (DestroyUsedItemMesh(npcContainer)) + // The NPC holds one used item, like the engine's state item - a new one replaces, never adds. + var props = npcContainer.Props; + 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); + 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); + } - _vobService.CreateItemMesh(npcContainer.Props.CurrentItem, slotGo); + private static GameObject FindSlot(NpcContainer npcContainer, string slot) + { + var bip01 = npcContainer.PrefabProps?.Bip01; + if (bip01 == null || string.IsNullOrEmpty(slot)) + return null; - npcContainer.Props.UsedItemSlot = slot1; + 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); } /// 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.