2023-03-06 06:12:08 +13:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-08-25 12:48:27 +10:00
|
|
|
using Content.Shared.Hands.Components;
|
Refactor antag rule code (#23445)
* Initial Pass, Rev, Thief
* Zombie initial pass
* Rebase, Traitor
* Nukeops, More overloads
* Revert RevolutionaryRuleComponent
* Use TryRoundStartAttempt, Rewrite nukie spawning
* Comments, Add task scheduler to GameRuleSystem
* Zombie initial testing done
* Sort methods, rework GameRuleTask
* Add CCVar, Initial testing continues
* Might as well get rid of the obsolete logging
* Oops, i dont know how to log apparently
* Suggested formatting fixes
* Suggested changes
* Fix merge issues
* Minor optimisation
* Allowed thief to choose other antags
* Review changes
* Spawn items on floor first, then inserting
* minor tweaks
* Shift as much as possible to ProtoId<>
* Remove unneeded
* Add exclusive antag attribute
* Fix merge issues
* Minor formatting fix
* Convert to struct
* Cleanup
* Review cleanup (need to test a lot)
* Some fixes, (mostly) tested
* oop
* Pass tests (for real)
---------
Co-authored-by: Rainfall <rainfey0+git@gmail.com>
Co-authored-by: AJCM <AJCM@tutanota.com>
2024-02-29 06:25:10 +00:00
|
|
|
using Content.Shared.Storage.EntitySystems;
|
2021-12-30 22:56:10 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
|
|
|
|
|
namespace Content.Shared.Inventory;
|
|
|
|
|
|
|
|
|
|
public partial class InventorySystem
|
|
|
|
|
{
|
Refactor antag rule code (#23445)
* Initial Pass, Rev, Thief
* Zombie initial pass
* Rebase, Traitor
* Nukeops, More overloads
* Revert RevolutionaryRuleComponent
* Use TryRoundStartAttempt, Rewrite nukie spawning
* Comments, Add task scheduler to GameRuleSystem
* Zombie initial testing done
* Sort methods, rework GameRuleTask
* Add CCVar, Initial testing continues
* Might as well get rid of the obsolete logging
* Oops, i dont know how to log apparently
* Suggested formatting fixes
* Suggested changes
* Fix merge issues
* Minor optimisation
* Allowed thief to choose other antags
* Review changes
* Spawn items on floor first, then inserting
* minor tweaks
* Shift as much as possible to ProtoId<>
* Remove unneeded
* Add exclusive antag attribute
* Fix merge issues
* Minor formatting fix
* Convert to struct
* Cleanup
* Review cleanup (need to test a lot)
* Some fixes, (mostly) tested
* oop
* Pass tests (for real)
---------
Co-authored-by: Rainfall <rainfey0+git@gmail.com>
Co-authored-by: AJCM <AJCM@tutanota.com>
2024-02-29 06:25:10 +00:00
|
|
|
[Dependency] private readonly SharedStorageSystem _storageSystem = default!;
|
|
|
|
|
|
2023-08-25 12:48:27 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Yields all entities in hands or inventory slots with the specific flags.
|
|
|
|
|
/// </summary>
|
2023-12-07 16:20:51 -05:00
|
|
|
public IEnumerable<EntityUid> GetHandOrInventoryEntities(Entity<HandsComponent?, InventoryComponent?> user, SlotFlags flags = SlotFlags.All)
|
2023-08-25 12:48:27 +10:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
if (Resolve(user.Owner, ref user.Comp1, false))
|
2023-08-25 12:48:27 +10:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
foreach (var hand in user.Comp1.Hands.Values)
|
2023-08-25 12:48:27 +10:00
|
|
|
{
|
|
|
|
|
if (hand.HeldEntity == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
yield return hand.HeldEntity.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 16:20:51 -05:00
|
|
|
if (!Resolve(user.Owner, ref user.Comp2, false))
|
|
|
|
|
yield break;
|
2023-08-25 12:48:27 +10:00
|
|
|
|
2023-12-07 16:20:51 -05:00
|
|
|
var slotEnumerator = new InventorySlotEnumerator(user.Comp2, flags);
|
|
|
|
|
while (slotEnumerator.NextItem(out var item))
|
|
|
|
|
{
|
|
|
|
|
yield return item;
|
2023-08-25 12:48:27 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 06:12:08 +13:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the definition of the inventory slot that the given entity is currently in..
|
|
|
|
|
/// </summary>
|
2023-12-07 16:20:51 -05:00
|
|
|
public bool TryGetContainingSlot(Entity<TransformComponent?, MetaDataComponent?> entity, [NotNullWhen(true)] out SlotDefinition? slot)
|
2023-03-06 06:12:08 +13:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
if (!_containerSystem.TryGetContainingContainer(entity.Owner, out var container, entity.Comp2, entity.Comp1))
|
2023-03-06 06:12:08 +13:00
|
|
|
{
|
|
|
|
|
slot = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TryGetSlot(container.Owner, container.ID, out slot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if the given entity is equipped to an inventory slot with the given inventory slot flags.
|
|
|
|
|
/// </summary>
|
2023-12-07 16:20:51 -05:00
|
|
|
public bool InSlotWithFlags(Entity<TransformComponent?, MetaDataComponent?> entity, SlotFlags flags)
|
2023-03-06 06:12:08 +13:00
|
|
|
{
|
2023-12-07 16:20:51 -05:00
|
|
|
return TryGetContainingSlot(entity, out var slot)
|
|
|
|
|
&& (slot.SlotFlags & flags) == flags;
|
2023-03-06 06:12:08 +13:00
|
|
|
}
|
|
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
public bool SpawnItemInSlot(EntityUid uid, string slot, string prototype, bool silent = false, bool force = false, InventoryComponent? inventory = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref inventory, false))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Let's do nothing if the owner of the inventory has been deleted.
|
|
|
|
|
if (Deleted(uid))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// If we don't have that slot or there's already an item there, we do nothing.
|
|
|
|
|
if (!HasSlot(uid, slot) || TryGetSlotEntity(uid, slot, out _, inventory))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// If the prototype in question doesn't exist, we do nothing.
|
|
|
|
|
if (!_prototypeManager.HasIndex<EntityPrototype>(prototype))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Let's spawn this first...
|
|
|
|
|
var item = EntityManager.SpawnEntity(prototype, Transform(uid).Coordinates);
|
|
|
|
|
|
|
|
|
|
// Helper method that deletes the item and returns false.
|
|
|
|
|
bool DeleteItem()
|
|
|
|
|
{
|
|
|
|
|
EntityManager.DeleteEntity(item);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We finally try to equip the item, otherwise we delete it.
|
|
|
|
|
return TryEquip(uid, item, slot, silent, force) || DeleteItem();
|
|
|
|
|
}
|
Refactor antag rule code (#23445)
* Initial Pass, Rev, Thief
* Zombie initial pass
* Rebase, Traitor
* Nukeops, More overloads
* Revert RevolutionaryRuleComponent
* Use TryRoundStartAttempt, Rewrite nukie spawning
* Comments, Add task scheduler to GameRuleSystem
* Zombie initial testing done
* Sort methods, rework GameRuleTask
* Add CCVar, Initial testing continues
* Might as well get rid of the obsolete logging
* Oops, i dont know how to log apparently
* Suggested formatting fixes
* Suggested changes
* Fix merge issues
* Minor optimisation
* Allowed thief to choose other antags
* Review changes
* Spawn items on floor first, then inserting
* minor tweaks
* Shift as much as possible to ProtoId<>
* Remove unneeded
* Add exclusive antag attribute
* Fix merge issues
* Minor formatting fix
* Convert to struct
* Cleanup
* Review cleanup (need to test a lot)
* Some fixes, (mostly) tested
* oop
* Pass tests (for real)
---------
Co-authored-by: Rainfall <rainfey0+git@gmail.com>
Co-authored-by: AJCM <AJCM@tutanota.com>
2024-02-29 06:25:10 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Will attempt to spawn a list of items inside of an entities bag, pockets, hands or nearby
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entity">The entity that you want to spawn an item on</param>
|
|
|
|
|
/// <param name="items">A list of prototype IDs that you want to spawn in the bag.</param>
|
2024-04-24 21:31:45 -04:00
|
|
|
public void SpawnItemsOnEntity(EntityUid entity, List<string> items)
|
Refactor antag rule code (#23445)
* Initial Pass, Rev, Thief
* Zombie initial pass
* Rebase, Traitor
* Nukeops, More overloads
* Revert RevolutionaryRuleComponent
* Use TryRoundStartAttempt, Rewrite nukie spawning
* Comments, Add task scheduler to GameRuleSystem
* Zombie initial testing done
* Sort methods, rework GameRuleTask
* Add CCVar, Initial testing continues
* Might as well get rid of the obsolete logging
* Oops, i dont know how to log apparently
* Suggested formatting fixes
* Suggested changes
* Fix merge issues
* Minor optimisation
* Allowed thief to choose other antags
* Review changes
* Spawn items on floor first, then inserting
* minor tweaks
* Shift as much as possible to ProtoId<>
* Remove unneeded
* Add exclusive antag attribute
* Fix merge issues
* Minor formatting fix
* Convert to struct
* Cleanup
* Review cleanup (need to test a lot)
* Some fixes, (mostly) tested
* oop
* Pass tests (for real)
---------
Co-authored-by: Rainfall <rainfey0+git@gmail.com>
Co-authored-by: AJCM <AJCM@tutanota.com>
2024-02-29 06:25:10 +00:00
|
|
|
{
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
SpawnItemOnEntity(entity, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Will attempt to spawn an item inside of an entities bag, pockets, hands or nearby
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entity">The entity that you want to spawn an item on</param>
|
|
|
|
|
/// <param name="item">The prototype ID that you want to spawn in the bag.</param>
|
|
|
|
|
public void SpawnItemOnEntity(EntityUid entity, EntProtoId item)
|
|
|
|
|
{
|
|
|
|
|
//Transform() throws error if TransformComponent doesnt exist
|
|
|
|
|
if (!HasComp<TransformComponent>(entity))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var xform = Transform(entity);
|
|
|
|
|
var mapCoords = _transform.GetMapCoordinates(xform);
|
|
|
|
|
|
|
|
|
|
var itemToSpawn = Spawn(item, mapCoords);
|
|
|
|
|
|
|
|
|
|
//Try insert into the backpack
|
|
|
|
|
if (TryGetSlotContainer(entity, "back", out var backSlot, out _)
|
|
|
|
|
&& backSlot.ContainedEntity.HasValue
|
|
|
|
|
&& _storageSystem.Insert(backSlot.ContainedEntity.Value, itemToSpawn, out _)
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Try insert into pockets
|
|
|
|
|
if (TryGetSlotContainer(entity, "pocket1", out var pocket1, out _)
|
|
|
|
|
&& _containerSystem.Insert(itemToSpawn, pocket1)
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (TryGetSlotContainer(entity, "pocket2", out var pocket2, out _)
|
|
|
|
|
&& _containerSystem.Insert(itemToSpawn, pocket2)
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//Try insert into hands, or drop on the floor
|
|
|
|
|
_handsSystem.PickupOrDrop(entity, itemToSpawn, false);
|
|
|
|
|
}
|
2021-12-30 22:56:10 +01:00
|
|
|
}
|