From 07d3d69040a1958e38b931af47f739cb41842acb Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Fri, 2 Mar 2018 13:45:58 +0200 Subject: [PATCH] Equipped items can be swapped by double-clicking, fixed inventories getting messed up when swapping multi-slot items fails (e.g. drag a jumpsuit on an equipped diving suit). --- .../Source/Items/CharacterInventory.cs | 16 +++++- .../Source/Items/Inventory.cs | 2 +- .../Source/Screens/SubEditorScreen.cs | 2 +- .../AI/Objectives/AIObjectiveCombat.cs | 2 +- .../AI/Objectives/AIObjectiveGetItem.cs | 2 +- .../AI/Objectives/AIObjectiveOperateItem.cs | 2 +- .../Source/Characters/Character.cs | 2 +- .../Source/Characters/HuskInfection.cs | 2 +- .../Source/Items/CharacterInventory.cs | 51 ++++++++++++++----- .../Source/Items/Components/ItemContainer.cs | 2 +- .../Source/Items/Inventory.cs | 6 +-- .../BarotraumaShared/Source/Items/Item.cs | 2 +- .../Source/Items/ItemInventory.cs | 4 +- 13 files changed, 65 insertions(+), 30 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs b/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs index c7dec1046..e44349106 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/CharacterInventory.cs @@ -127,7 +127,6 @@ namespace Barotrauma if (doubleClickedItem != null) { bool wasPut = false; - if (doubleClickedItem.ParentInventory != this) { wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots, true); @@ -152,7 +151,12 @@ namespace Barotrauma //not equipped -> attempt to equip if (IsInLimbSlot(doubleClickedItem, InvSlotType.Any)) { - wasPut = TryPutItem(doubleClickedItem, Character.Controlled, doubleClickedItem.AllowedSlots.FindAll(i => i != InvSlotType.Any), true); + for (int i = 0; i < capacity; i++) + { + if (limbSlots[i] == InvSlotType.Any || !doubleClickedItem.AllowedSlots.Any(a => a.HasFlag(limbSlots[i]))) continue; + wasPut = TryPutItem(doubleClickedItem, i, true, false, Character.Controlled, true); + if (wasPut) break; + } } //equipped -> attempt to unequip else if (doubleClickedItem.AllowedSlots.Contains(InvSlotType.Any)) @@ -162,6 +166,14 @@ namespace Barotrauma } } + if (wasPut) + { + for (int i = 0; i < capacity; i++) + { + if (Items[i] == doubleClickedItem) slots[i].ShowBorderHighlight(Color.Green, 0.1f, 0.9f); + } + } + draggingItem = null; GUI.PlayUISound(wasPut ? GUISoundType.PickItem : GUISoundType.PickItemFail); } diff --git a/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs b/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs index 97d940495..7b1028a69 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Inventory.cs @@ -386,7 +386,7 @@ namespace Barotrauma { Inventory selectedInventory = selectedSlot.Inventory; int slotIndex = selectedSlot.SlotIndex; - if (selectedInventory.TryPutItem(draggingItem, slotIndex, true, Character.Controlled)) + if (selectedInventory.TryPutItem(draggingItem, slotIndex, true, true, Character.Controlled)) { if (selectedInventory.slots != null) selectedInventory.slots[slotIndex].ShowBorderHighlight(Color.White, 0.1f, 0.4f); GUI.PlayUISound(GUISoundType.PickItem); diff --git a/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs b/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs index 4bfd11254..5d78872a5 100644 --- a/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs +++ b/Barotrauma/BarotraumaClient/Source/Screens/SubEditorScreen.cs @@ -936,7 +936,7 @@ namespace Barotrauma existingWire.Remove(); } - dummyCharacter.Inventory.TryPutItem(wire, slotIndex, false, dummyCharacter); + dummyCharacter.Inventory.TryPutItem(wire, slotIndex, false, false, dummyCharacter); return true; diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs index cf84ae4f3..84c807617 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs @@ -50,7 +50,7 @@ namespace Barotrauma //TODO: make sure the weapon is ready to use (projectiles/batteries loaded) if (!character.SelectedItems.Contains(weapon)) { - if (character.Inventory.TryPutItem(weapon, 3, false, character)) + if (character.Inventory.TryPutItem(weapon, 3, false, false, character)) { weapon.Equip(character); } diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveGetItem.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveGetItem.cs index 5f7d81577..1234b9044 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveGetItem.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveGetItem.cs @@ -119,7 +119,7 @@ namespace Barotrauma if (targetSlot > -1 && character.Inventory.IsInLimbSlot(targetItem, InvSlotType.Any)) { - character.Inventory.TryPutItem(targetItem, targetSlot, false, character); + character.Inventory.TryPutItem(targetItem, targetSlot, false, false, character); } } else diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveOperateItem.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveOperateItem.cs index 061442e73..2e584ebec 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveOperateItem.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveOperateItem.cs @@ -113,7 +113,7 @@ namespace Barotrauma character.Inventory.Items[i].Drop(); } } - if (character.Inventory.TryPutItem(component.Item, i, true, character)) + if (character.Inventory.TryPutItem(component.Item, i, true, false, character)) { component.Item.Equip(character); break; diff --git a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs index a7859797c..dff432aeb 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs @@ -657,7 +657,7 @@ namespace Barotrauma if (item == null) continue; item.TryInteract(this, true, true, true); - inventory.TryPutItem(item, i, false, null, false); + inventory.TryPutItem(item, i, false, false, null, false); } } } diff --git a/Barotrauma/BarotraumaShared/Source/Characters/HuskInfection.cs b/Barotrauma/BarotraumaShared/Source/Characters/HuskInfection.cs index 08efbda6c..bc556102c 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/HuskInfection.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/HuskInfection.cs @@ -214,7 +214,7 @@ namespace Barotrauma for (int i = 0; i < character.Inventory.Items.Length; i++) { if (character.Inventory.Items[i] == null) continue; - husk.Inventory.TryPutItem(character.Inventory.Items[i], i, true, null); + husk.Inventory.TryPutItem(character.Inventory.Items[i], i, true, false, null); } yield return CoroutineStatus.Success; diff --git a/Barotrauma/BarotraumaShared/Source/Items/CharacterInventory.cs b/Barotrauma/BarotraumaShared/Source/Items/CharacterInventory.cs index 00483a2e4..45cb782f7 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/CharacterInventory.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/CharacterInventory.cs @@ -113,7 +113,7 @@ namespace Barotrauma bool free = true; for (int i = 0; i < capacity; i++) { - if (allowedSlot.HasFlag(limbSlots[i]) && Items[i]!=null && Items[i]!=item) + if (allowedSlot.HasFlag(limbSlots[i]) && Items[i] != null && Items[i] != item) { free = false; #if CLIENT @@ -144,7 +144,7 @@ namespace Barotrauma return placed; } - public override bool TryPutItem(Item item, int index, bool allowSwapping, Character user, bool createNetworkEvent = true) + public override bool TryPutItem(Item item, int index, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true) { //there's already an item in the slot if (Items[index] != null) @@ -152,7 +152,7 @@ namespace Barotrauma if (Items[index] == item) return false; bool combined = false; - if (Items[index].Combine(item)) + if (allowCombine && Items[index].Combine(item)) { System.Diagnostics.Debug.Assert(Items[index] != null); @@ -168,25 +168,48 @@ namespace Barotrauma else if (item.ParentInventory == this && allowSwapping) { int currentIndex = Array.IndexOf(Items, item); - + Item existingItem = Items[index]; - Items[currentIndex] = null; - Items[index] = null; - //if the item in the slot can be moved to the slot of the moved item - if (TryPutItem(existingItem, currentIndex, false, user, createNetworkEvent) && - TryPutItem(item, index, false, user, createNetworkEvent)) + for (int i = 0; i < capacity; i++) { - + if (Items[i] == item || Items[i] == existingItem) Items[i] = null; + } + + //if the item in the slot can be moved to the slot of the moved item + if (TryPutItem(existingItem, currentIndex, false, false, user, createNetworkEvent) && + TryPutItem(item, index, false, false, user, createNetworkEvent)) + { +#if CLIENT + for (int i = 0; i < capacity; i++) + { + if (Items[i] == item || Items[i] == existingItem) + { + slots[i].ShowBorderHighlight(Color.Green, 0.1f, 0.9f); + } + } +#endif } else { - Items[currentIndex] = null; - Items[index] = null; + for (int i = 0; i < capacity; i++) + { + if (Items[i] == item || Items[i] == existingItem) Items[i] = null; + } //swapping the items failed -> move them back to where they were - TryPutItem(item, currentIndex, false, user, createNetworkEvent); - TryPutItem(existingItem, index, false, user, createNetworkEvent); + TryPutItem(item, currentIndex, false, false, user, createNetworkEvent); + TryPutItem(existingItem, index, false, false, user, createNetworkEvent); +#if CLIENT + for (int i = 0; i < capacity; i++) + { + if (Items[i] == existingItem) + { + slots[i].ShowBorderHighlight(Color.Red, 0.1f, 0.9f); + } + } +#endif + } } diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs index 30923113c..9016ba8fd 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs @@ -204,7 +204,7 @@ namespace Barotrauma.Items.Components Item item = Entity.FindEntityByID(itemIds[i]) as Item; if (item == null) continue; - Inventory.TryPutItem(item, i, false, null, false); + Inventory.TryPutItem(item, i, false, false, null, false); } itemIds = null; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs b/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs index b885e7b25..eedb8504a 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Inventory.cs @@ -95,7 +95,7 @@ namespace Barotrauma return true; } - public virtual bool TryPutItem(Item item, int i, bool allowSwapping, Character user, bool createNetworkEvent = true) + public virtual bool TryPutItem(Item item, int i, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true) { if (Owner == null) return false; if (CanBePut(item, i)) @@ -224,7 +224,7 @@ namespace Barotrauma { if (!item.CanClientAccess(c)) continue; } - TryPutItem(item, i, true, c.Character, false); + TryPutItem(item, i, true, true, c.Character, false); } } @@ -321,7 +321,7 @@ namespace Barotrauma var item = Entity.FindEntityByID(receivedItemIDs[i]) as Item; if (item == null) continue; - TryPutItem(item, i, true, null, false); + TryPutItem(item, i, true, true, null, false); } } diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index eb4f3fc5d..53d7bf7e8 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -1642,7 +1642,7 @@ namespace Barotrauma if (inventory != null) { if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 && - inventory.TryPutItem(item, inventorySlotIndex, false, null, false)) + inventory.TryPutItem(item, inventorySlotIndex, false, false, null, false)) { return null; } diff --git a/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs b/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs index d297e58ef..728eb9390 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/ItemInventory.cs @@ -63,9 +63,9 @@ namespace Barotrauma return wasPut; } - public override bool TryPutItem(Item item, int i, bool allowSwapping, Character user, bool createNetworkEvent = true) + public override bool TryPutItem(Item item, int i, bool allowSwapping, bool allowCombine, Character user, bool createNetworkEvent = true) { - bool wasPut = base.TryPutItem(item, i, allowSwapping, user, createNetworkEvent); + bool wasPut = base.TryPutItem(item, i, allowSwapping, allowCombine, user, createNetworkEvent); if (wasPut) {