From b59dbfb079471c346566821d6cb2a593eb9eb6fc Mon Sep 17 00:00:00 2001 From: Jade-Harleyy <67431770+Jade-Harleyy@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:03:32 -0500 Subject: [PATCH 1/3] Add 'SpawnPositionType.TargetInventory' and let ItemSpawnInfo.Equip work with all valid SpawnPositionTypes. --- .../StatusEffects/StatusEffect.cs | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index e7d287f790..51c0466789 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -151,7 +151,11 @@ public enum SpawnPositionType /// /// The position of the entity the StatusEffect is targeting. If there are multiple targets, an item is spawned at all of them. /// - Target + Target, + /// + /// The inventories of the entities the StatusEffect is targeting. If there are multiple targets, the items are spawned in all of them. + /// + TargetInventory } public enum SpawnRotationType @@ -2138,7 +2142,7 @@ entity is Item item && void ProcessItemSpawnInfo(ItemSpawnInfo spawnInfo) { - if (spawnInfo.SpawnPosition == ItemSpawnInfo.SpawnPositionType.Target) + if (spawnInfo.SpawnPosition is ItemSpawnInfo.SpawnPositionType.Target or ItemSpawnInfo.SpawnPositionType.TargetInventory) { foreach (var target in targets) { @@ -2192,7 +2196,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou SetUser(parentItem.GetComponent()?.User); } - if (chosenItemSpawnInfo.SpawnPosition == ItemSpawnInfo.SpawnPositionType.Target && targetEntity != null) + if (chosenItemSpawnInfo.SpawnPosition is ItemSpawnInfo.SpawnPositionType.Target or ItemSpawnInfo.SpawnPositionType.TargetInventory && targetEntity != null) { entity = targetEntity; position = entity.WorldPosition; @@ -2318,6 +2322,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou }); break; case ItemSpawnInfo.SpawnPositionType.ThisInventory: + case ItemSpawnInfo.SpawnPositionType.TargetInventory: { Inventory inventory = null; if (entity is Character character && character.Inventory != null) @@ -2343,16 +2348,6 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou { Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: item => { - if (chosenItemSpawnInfo.Equip && entity is Character character && character.Inventory != null) - { - //if the item is both pickable and wearable, try to wear it instead of picking it up - List allowedSlots = - item.GetComponents().Count() > 1 ? - new List(item.GetComponent()?.AllowedSlots ?? item.GetComponent().AllowedSlots) : - new List(item.AllowedSlots); - allowedSlots.Remove(InvSlotType.Any); - character.Inventory.TryPutItem(item, null, allowedSlots); - } OnItemSpawned(item, chosenItemSpawnInfo); }); } @@ -2419,8 +2414,19 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou } break; } + void OnItemSpawned(Item newItem, ItemSpawnInfo itemSpawnInfo) { + if (itemSpawnInfo.SpawnPosition is not ItemSpawnInfo.SpawnPositionType.This or ItemSpawnInfo.SpawnPositionType.Target && itemSpawnInfo.Equip && entity is Character character && character.Inventory != null) + { + //if the item is both pickable and wearable, try to wear it instead of picking it up + List allowedSlots = newItem.GetComponents().Count() > 1 + ? new List(newItem.GetComponent()?.AllowedSlots ?? newItem.GetComponent().AllowedSlots) + : new List(newItem.AllowedSlots); + allowedSlots.Remove(InvSlotType.Any); + character.Inventory.TryPutItem(newItem, null, allowedSlots); + } + newItem.Condition = newItem.MaxCondition * itemSpawnInfo.Condition; if (itemSpawnInfo.InheritEventTags) { From 123ffe20f9c04b8d1e6c3f7c6310a043b8dcb492 Mon Sep 17 00:00:00 2001 From: Jade-Harleyy <67431770+Jade-Harleyy@users.noreply.github.com> Date: Thu, 8 Aug 2024 22:01:14 -0500 Subject: [PATCH 2/3] oops! --- .../StatusEffects/StatusEffect.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index 51c0466789..0c352b468a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -10,6 +10,7 @@ using System.Collections.Immutable; using System.Linq; using System.Xml.Linq; +using static Barotrauma.EntitySpawner; namespace Barotrauma { @@ -2348,6 +2349,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou { Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: item => { + if (chosenItemSpawnInfo.Equip) TryEquipItem(item); OnItemSpawned(item, chosenItemSpawnInfo); }); } @@ -2368,6 +2370,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou { Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: (Item newItem) => { + if (chosenItemSpawnInfo.Equip) TryEquipItem(newItem); OnItemSpawned(newItem, chosenItemSpawnInfo); }); } @@ -2415,18 +2418,20 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou break; } - void OnItemSpawned(Item newItem, ItemSpawnInfo itemSpawnInfo) + void TryEquipItem(Item newItem) { - if (itemSpawnInfo.SpawnPosition is not ItemSpawnInfo.SpawnPositionType.This or ItemSpawnInfo.SpawnPositionType.Target && itemSpawnInfo.Equip && entity is Character character && character.Inventory != null) - { - //if the item is both pickable and wearable, try to wear it instead of picking it up - List allowedSlots = newItem.GetComponents().Count() > 1 - ? new List(newItem.GetComponent()?.AllowedSlots ?? newItem.GetComponent().AllowedSlots) - : new List(newItem.AllowedSlots); - allowedSlots.Remove(InvSlotType.Any); - character.Inventory.TryPutItem(newItem, null, allowedSlots); - } + if (entity is not Character { Inventory: not null } character) return; + // If the item is both pickable and wearable, try to wear it instead of picking it up. + List allowedSlots = newItem.GetComponents().Count() > 1 + ? new List(newItem.GetComponent()?.AllowedSlots ?? newItem.GetComponent().AllowedSlots) + : new List(newItem.AllowedSlots); + allowedSlots.Remove(InvSlotType.Any); + character.Inventory.TryPutItem(newItem, null, allowedSlots); + } + + void OnItemSpawned(Item newItem, ItemSpawnInfo itemSpawnInfo) + { newItem.Condition = newItem.MaxCondition * itemSpawnInfo.Condition; if (itemSpawnInfo.InheritEventTags) { From 2b5497e17995090ccc765f7fc98a437f283d393c Mon Sep 17 00:00:00 2001 From: Jade-Harleyy <67431770+Jade-Harleyy@users.noreply.github.com> Date: Fri, 20 Sep 2024 02:50:02 -0500 Subject: [PATCH 3/3] Fix equipping not actually working. --- .../SharedSource/StatusEffects/StatusEffect.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs index 0c352b468a..d0a90f40a4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/StatusEffects/StatusEffect.cs @@ -2349,7 +2349,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou { Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: item => { - if (chosenItemSpawnInfo.Equip) TryEquipItem(item); + if (chosenItemSpawnInfo.Equip) TryEquipItem(item, inventory); OnItemSpawned(item, chosenItemSpawnInfo); }); } @@ -2370,7 +2370,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou { Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: (Item newItem) => { - if (chosenItemSpawnInfo.Equip) TryEquipItem(newItem); + if (chosenItemSpawnInfo.Equip) TryEquipItem(newItem, inventory); OnItemSpawned(newItem, chosenItemSpawnInfo); }); } @@ -2418,16 +2418,15 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou break; } - void TryEquipItem(Item newItem) + void TryEquipItem(Item newItem, Inventory inventory) { - if (entity is not Character { Inventory: not null } character) return; + if (inventory is not CharacterInventory charInventory) { return; } - // If the item is both pickable and wearable, try to wear it instead of picking it up. - List allowedSlots = newItem.GetComponents().Count() > 1 - ? new List(newItem.GetComponent()?.AllowedSlots ?? newItem.GetComponent().AllowedSlots) - : new List(newItem.AllowedSlots); + // If the item is wearable, try to wear it instead of picking it up. + IEnumerable wearableSlots = newItem.GetComponents().SelectMany(wearable => wearable.AllowedSlots).Distinct(); + List allowedSlots = (wearableSlots.Any() ? wearableSlots : newItem.AllowedSlots).ToList(); allowedSlots.Remove(InvSlotType.Any); - character.Inventory.TryPutItem(newItem, null, allowedSlots); + charInventory.TryPutItem(newItem, null, allowedSlots); } void OnItemSpawned(Item newItem, ItemSpawnInfo itemSpawnInfo)