Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ 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);
}

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);
}
Expand Down
25 changes: 25 additions & 0 deletions Assets/Gothic-Core/Scripts/Services/Npc/NpcInventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ public void EquipBestArmor(NpcInstance npc)
EquipArmorFromInventory(npc, best);
}

/// <summary>
/// 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.
/// </summary>
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);
}

/// <inheritdoc cref="EquipDomain.RemoveEquippedWeapon"/>
public void RemoveEquippedWeapon(NpcContainer npc, string itemInstanceName)
{
Expand Down