Inventories (#61)

* Bully the fuck out of inventory shit
Inventory Stuff
Inventories technically work
It works technicaly speaking
Yeah this part too
Lets do it!
Inventories completed
Motherfucker

* Remove unnecessary usings and fix one thing

* Submodule update

* Adds a bunch of various clothing prototypes for each current inventory slot
This commit is contained in:
clusterfack
2018-04-25 06:42:35 -05:00
committed by Pieter-Jan Briers
parent ea05c593aa
commit c33c227d95
37 changed files with 1205 additions and 395 deletions

View File

@@ -0,0 +1,273 @@
using SS14.Server.GameObjects;
using SS14.Server.GameObjects.Components.Container;
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects;
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.Network;
using static Content.Shared.GameObjects.SharedInventoryComponent.ClientInventoryMessage;
using static Content.Shared.GameObjects.SharedInventoryComponent.ServerInventoryMessage;
using SS14.Shared.IoC;
using SS14.Server.Interfaces.Player;
using SS14.Shared.GameObjects.Serialization;
using SS14.Shared.ContentPack;
namespace Content.Server.GameObjects
{
public class InventoryComponent : SharedInventoryComponent
{
private Dictionary<Slots, ContainerSlot> SlotContainers = new Dictionary<Slots, ContainerSlot>();
string TemplateName = "HumanInventory"; //stored for serialization purposes
public override void ExposeData(EntitySerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref TemplateName, "Template", "HumanInventory");
CreateInventory(TemplateName);
}
private void CreateInventory(string TemplateName)
{
Type type = AppDomain.CurrentDomain.GetAssemblyByName("Content.Shared").GetType("Content.Shared.GameObjects." + TemplateName);
Inventory inventory = (Inventory)Activator.CreateInstance(type);
foreach (Slots slotnames in inventory.SlotMasks)
{
if(slotnames != Slots.NONE)
{
var newslot = AddSlot(slotnames);
}
}
}
public override void OnRemove()
{
foreach (var slot in SlotContainers.Keys)
{
RemoveSlot(slot);
}
base.OnRemove();
}
/// <summary>
/// Helper to get container name for specified slot on this component
/// </summary>
/// <param name="slot"></param>
/// <returns></returns>
private string GetSlotString(Slots slot)
{
return Name + "_" + Enum.GetName(typeof(Slots), slot);
}
/// <summary>
/// Gets the clothing equipped to the specified slot.
/// </summary>
/// <param name="slot">The slot to get the item for.</param>
/// <returns>Null if the slot is empty, otherwise the item.</returns>
public ItemComponent GetSlotItem(Slots slot)
{
return SlotContainers[slot].ContainedEntity?.GetComponent<ItemComponent>();
}
/// <summary>
/// Equips slothing to the specified slot.
/// </summary>
/// <remarks>
/// This will fail if there is already an item in the specified slot.
/// </remarks>
/// <param name="slot">The slot to put the item in.</param>
/// <param name="clothing">The item to insert into the slot.</param>
/// <returns>True if the item was successfully inserted, false otherwise.</returns>
public bool Equip(Slots slot, ClothingComponent clothing)
{
if (clothing == null)
{
throw new ArgumentNullException(nameof(clothing), "Clothing must be passed here. To remove some clothing from a slot, use Unequip()");
}
if(clothing.SlotFlags == SlotFlags.PREVENTEQUIP //Flag to prevent equipping at all
|| (clothing.SlotFlags & SlotMasks[slot]) == 0) //Does the clothing flag have any of our requested slot flags
{
return false;
}
var inventorySlot = SlotContainers[slot];
if (!inventorySlot.Insert(clothing.Owner))
{
return false;
}
clothing.EquippedToSlot(inventorySlot);
var UIupdatemessage = new ServerInventoryMessage()
{
Inventoryslot = slot,
EntityUid = clothing.Owner.Uid,
Updatetype = ServerInventoryUpdate.Addition
};
SendNetworkMessage(UIupdatemessage);
return true;
}
/// <summary>
/// Checks whether an item can be put in the specified slot.
/// </summary>
/// <param name="slot">The slot to check for.</param>
/// <param name="item">The item to check for.</param>
/// <returns>True if the item can be inserted into the specified slot.</returns>
public bool CanEquip(Slots slot, ClothingComponent item)
{
return SlotContainers[slot].CanInsert(item.Owner);
}
/// <summary>
/// Drops the item in a slot.
/// </summary>
/// <param name="slot">The slot to drop the item from.</param>
/// <returns>True if an item was dropped, false otherwise.</returns>
public bool Unequip(Slots slot)
{
if (!CanUnequip(slot))
{
return false;
}
var inventorySlot = SlotContainers[slot];
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
if (!inventorySlot.Remove(inventorySlot.ContainedEntity))
{
return false;
}
var UIupdatemessage = new ServerInventoryMessage()
{
Inventoryslot = slot,
EntityUid = item.Owner.Uid,
Updatetype = ServerInventoryUpdate.Removal
};
SendNetworkMessage(UIupdatemessage);
item.RemovedFromSlot();
// TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<TransformComponent>();
itemTransform.LocalPosition = Owner.GetComponent<TransformComponent>().LocalPosition;
return true;
}
/// <summary>
/// Checks whether an item can be dropped from the specified slot.
/// </summary>
/// <param name="slot">The slot to check for.</param>
/// <returns>
/// True if there is an item in the slot and it can be dropped, false otherwise.
/// </returns>
public bool CanUnequip(Slots slot)
{
var InventorySlot = SlotContainers[slot];
return InventorySlot.ContainedEntity != null && InventorySlot.CanRemove(InventorySlot.ContainedEntity);
}
/// <summary>
/// Adds a new slot to this inventory component.
/// </summary>
/// <param name="slot">The name of the slot to add.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if the slot with specified name already exists.
/// </exception>
public ContainerSlot AddSlot(Slots slot)
{
if (HasSlot(slot))
{
throw new InvalidOperationException($"Slot '{slot}' already exists.");
}
return SlotContainers[slot] = ContainerManagerComponent.Create<ContainerSlot>(GetSlotString(slot), Owner);
}
/// <summary>
/// Removes a slot from this inventory component.
/// </summary>
/// <remarks>
/// If the slot contains an item, the item is dropped.
/// </remarks>
/// <param name="slot">The name of the slot to remove.</param>
public void RemoveSlot(Slots slot)
{
if (!HasSlot(slot))
{
throw new InvalidOperationException($"Slow '{slot}' does not exist.");
}
if (GetSlotItem(slot) != null && !Unequip(slot))
{
// TODO: Handle this potential failiure better.
throw new InvalidOperationException("Unable to remove slot as the contained clothing could not be dropped");
}
SlotContainers.Remove(slot);
}
/// <summary>
/// Checks whether a slot with the specified name exists.
/// </summary>
/// <param name="slot">The slot name to check.</param>
/// <returns>True if the slot exists, false otherwise.</returns>
public bool HasSlot(Slots slot)
{
return SlotContainers.ContainsKey(slot);
}
/// <summary>
/// Message that tells us to equip or unequip items from the inventory slots
/// </summary>
/// <param name="msg"></param>
private void HandleInventoryMessage(ClientInventoryMessage msg)
{
if (msg.Updatetype == ClientInventoryUpdate.Equip)
{
var hands = Owner.GetComponent<HandsComponent>();
var activehand = hands.GetActiveHand;
if (activehand != null && activehand.Owner.TryGetComponent(out ClothingComponent clothing))
{
hands.Drop(hands.ActiveIndex);
if(!Equip(msg.Inventoryslot, clothing))
{
hands.PutInHand(clothing);
}
}
}
else if (msg.Updatetype == ClientInventoryUpdate.Unequip)
{
var hands = Owner.GetComponent<HandsComponent>();
var activehand = hands.GetActiveHand;
var itemcontainedinslot = GetSlotItem(msg.Inventoryslot);
if (activehand == null && itemcontainedinslot != null && Unequip(msg.Inventoryslot))
{
hands.PutInHand(itemcontainedinslot);
}
}
}
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null)
{
base.HandleMessage(message, netChannel, component);
switch(message)
{
case ClientInventoryMessage msg:
var playerMan = IoCManager.Resolve<IPlayerManager>();
var session = playerMan.GetSessionByChannel(netChannel);
var playerentity = session.AttachedEntity;
if (playerentity == Owner)
HandleInventoryMessage(msg);
break;
}
}
}
}

View File

@@ -3,15 +3,15 @@ using System.Collections.Generic;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects;
using SS14.Server.Interfaces.GameObjects;
using SS14.Server.GameObjects;
using SS14.Server.GameObjects.Components.Container;
using SS14.Server.Interfaces.Player;
using SS14.Shared.GameObjects;
using SS14.Shared.GameObjects.Serialization;
using SS14.Shared.Input;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.Network;
using SS14.Shared.IoC;
using SS14.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Server.GameObjects
{
@@ -33,60 +33,41 @@ namespace Content.Server.GameObjects
}
}
private Dictionary<string, IInventorySlot> hands = new Dictionary<string, IInventorySlot>();
private Dictionary<string, ContainerSlot> hands = new Dictionary<string, ContainerSlot>();
private List<string> orderedHands = new List<string>();
private IInventoryComponent inventory;
private IServerTransformComponent transform;
private YamlMappingNode tempParametersMapping;
// Mostly arbitrary.
public const float PICKUP_RANGE = 2;
public override void Initialize()
public override void ExposeData(EntitySerializer serializer)
{
inventory = Owner.GetComponent<IInventoryComponent>();
transform = Owner.GetComponent<IServerTransformComponent>();
if (tempParametersMapping != null)
base.ExposeData(serializer);
serializer.DataField(ref orderedHands, "hands", new List<string>(0));
foreach (var handsname in orderedHands)
{
foreach (var node in tempParametersMapping.GetNode<YamlSequenceNode>("hands"))
{
AddHand(node.AsString());
}
AddHand(handsname);
}
base.Initialize();
}
public override void OnRemove()
{
inventory = null;
base.OnRemove();
}
public override void LoadParameters(YamlMappingNode mapping)
{
tempParametersMapping = mapping;
base.LoadParameters(mapping);
}
public IEnumerable<IItemComponent> GetAllHeldItems()
public IEnumerable<ItemComponent> GetAllHeldItems()
{
foreach (var slot in hands.Values)
{
if (slot.Item != null)
if (slot.ContainedEntity != null)
{
yield return slot.Item;
yield return slot.ContainedEntity.GetComponent<ItemComponent>();
}
}
}
public IItemComponent GetHand(string index)
public ItemComponent GetHand(string index)
{
var slot = hands[index];
return slot.Item;
return slot.ContainedEntity?.GetComponent<ItemComponent>();
}
public IItemComponent GetActiveHand => GetHand(ActiveIndex);
public ItemComponent GetActiveHand => GetHand(ActiveIndex);
/// <summary>
/// Enumerates over the hand keys, returning the active hand first.
@@ -105,7 +86,7 @@ namespace Content.Server.GameObjects
}
}
public bool PutInHand(IItemComponent item)
public bool PutInHand(ItemComponent item)
{
foreach (var hand in ActivePriorityEnumerable())
{
@@ -118,7 +99,7 @@ namespace Content.Server.GameObjects
return false;
}
public bool PutInHand(IItemComponent item, string index, bool fallback = true)
public bool PutInHand(ItemComponent item, string index, bool fallback = true)
{
if (!CanPutInHand(item, index))
{
@@ -126,10 +107,10 @@ namespace Content.Server.GameObjects
}
var slot = hands[index];
return slot.Owner.Insert(slot.Name, item);
return slot.Insert(item.Owner);
}
public bool CanPutInHand(IItemComponent item)
public bool CanPutInHand(ItemComponent item)
{
foreach (var hand in ActivePriorityEnumerable())
{
@@ -142,27 +123,50 @@ namespace Content.Server.GameObjects
return false;
}
public bool CanPutInHand(IItemComponent item, string index)
public bool CanPutInHand(ItemComponent item, string index)
{
var slot = hands[index];
return slot.Owner.CanInsert(slot.Name, item);
return slot.CanInsert(item.Owner);
}
public bool Drop(string index)
/// <summary>
/// Drops the item in a slot.
/// </summary>
/// <param name="slot">The slot to drop the item from.</param>
/// <returns>True if an item was dropped, false otherwise.</returns>
public bool Drop(string slot)
{
if (!CanDrop(index))
if (!CanDrop(slot))
{
return false;
}
var slot = hands[index];
return slot.Owner.Drop(slot.Name);
var inventorySlot = hands[slot];
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
if (!inventorySlot.Remove(inventorySlot.ContainedEntity))
{
return false;
}
item.RemovedFromSlot();
// TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<TransformComponent>();
itemTransform.LocalPosition = Owner.GetComponent<TransformComponent>().LocalPosition;
return true;
}
public bool CanDrop(string index)
/// <summary>
/// Checks whether an item can be dropped from the specified slot.
/// </summary>
/// <param name="slot">The slot to check for.</param>
/// <returns>
/// True if there is an item in the slot and it can be dropped, false otherwise.
/// </returns>
public bool CanDrop(string slot)
{
var slot = hands[index];
return slot.Item != null && slot.Owner.CanDrop(slot.Name);
var inventorySlot = hands[slot];
return inventorySlot.CanRemove(inventorySlot.ContainedEntity);
}
public void AddHand(string index)
@@ -172,9 +176,12 @@ namespace Content.Server.GameObjects
throw new InvalidOperationException($"Hand '{index}' already exists.");
}
var slot = inventory.AddSlot(HandSlotName(index));
var slot = ContainerManagerComponent.Create<ContainerSlot>(Name + "_" + index, Owner);
hands[index] = slot;
orderedHands.Add(index);
if(!orderedHands.Contains(index))
{
orderedHands.Add(index);
}
if (ActiveIndex == null)
{
ActiveIndex = index;
@@ -188,7 +195,7 @@ namespace Content.Server.GameObjects
throw new InvalidOperationException($"Hand '{index}' does not exist.");
}
inventory.RemoveSlot(HandSlotName(index));
hands[index].Shutdown(); //TODO verify this
hands.Remove(index);
orderedHands.Remove(index);
@@ -220,9 +227,9 @@ namespace Content.Server.GameObjects
var dict = new Dictionary<string, EntityUid>(hands.Count);
foreach (var hand in hands)
{
if (hand.Value.Item != null)
if (hand.Value.ContainedEntity != null)
{
dict[hand.Key] = hand.Value.Item.Owner.Uid;
dict[hand.Key] = hand.Value.ContainedEntity.Uid;
}
}
return new HandsComponentState(dict, ActiveIndex);
@@ -239,6 +246,7 @@ namespace Content.Server.GameObjects
ActiveIndex = orderedHands[index];
}
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null)
{
@@ -271,7 +279,7 @@ namespace Content.Server.GameObjects
break;
}
//Boundkeychangedmsg only works for the player entity
//Boundkeychangedmsg only works for the player entity and doesn't need any extra verification
case BoundKeyChangedMsg msg:
if (msg.State != BoundKeyState.Down)
return;

View File

@@ -0,0 +1,29 @@
using SS14.Shared.GameObjects.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
namespace Content.Server.GameObjects
{
public class ClothingComponent : ItemComponent
{
public override string Name => "Clothing";
public SlotFlags SlotFlags = SlotFlags.PREVENTEQUIP; //Different from None, NONE allows equips if no slot flags are required
private List<string> slotstrings = new List<string>(); //serialization
public override void ExposeData(EntitySerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref slotstrings, "Slots", new List<string>(0));
foreach(var slotflagsloaded in slotstrings)
{
SlotFlags |= (SlotFlags)Enum.Parse(typeof(SlotFlags), slotflagsloaded.ToUpper());
}
}
}
}

View File

@@ -1,167 +0,0 @@
using Content.Server.Interfaces.GameObjects;
using SS14.Server.GameObjects;
using SS14.Server.GameObjects.Components.Container;
using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.Utility;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
using YamlDotNet.RepresentationModel;
namespace Content.Server.GameObjects
{
public class InventoryComponent : Component, IInventoryComponent
{
public override string Name => "Inventory";
private Dictionary<string, InventorySlot> slots = new Dictionary<string, InventorySlot>();
private TransformComponent transform;
// TODO: Make this container unique per-slot.
private IContainer container;
public override void Initialize()
{
transform = Owner.GetComponent<TransformComponent>();
container = Container.Create("inventory", Owner);
base.Initialize();
}
public override void OnRemove()
{
foreach (var slot in slots.Keys)
{
RemoveSlot(slot);
}
transform = null;
container = null;
base.OnRemove();
}
public override void LoadParameters(YamlMappingNode mapping)
{
if (mapping.TryGetNode<YamlSequenceNode>("slots", out var slotsNode))
{
foreach (var node in slotsNode)
{
AddSlot(node.AsString());
}
}
base.LoadParameters(mapping);
}
public IItemComponent Get(string slot)
{
return _GetSlot(slot).Item;
}
public IInventorySlot GetSlot(string slot)
{
return slots[slot];
}
// Private version that returns our concrete implementation.
private InventorySlot _GetSlot(string slot)
{
return slots[slot];
}
public bool Insert(string slot, IItemComponent item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item), "An item must be passed. To remove an item from a slot, use Drop()");
}
var inventorySlot = _GetSlot(slot);
if (!CanInsert(slot, item) || !container.Insert(item.Owner))
{
return false;
}
inventorySlot.Item = item;
item.EquippedToSlot(inventorySlot);
return true;
}
public bool CanInsert(string slot, IItemComponent item)
{
var inventorySlot = _GetSlot(slot);
return inventorySlot.Item == null && container.CanInsert(item.Owner);
}
public bool Drop(string slot)
{
if (!CanDrop(slot))
{
return false;
}
var inventorySlot = _GetSlot(slot);
var item = inventorySlot.Item;
if (!container.Remove(item.Owner))
{
return false;
}
item.RemovedFromSlot();
inventorySlot.Item = null;
// TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<TransformComponent>();
itemTransform.LocalPosition = transform.LocalPosition;
return true;
}
public bool CanDrop(string slot)
{
var inventorySlot = _GetSlot(slot);
var item = inventorySlot.Item;
return item != null && container.CanRemove(item.Owner);
}
public IInventorySlot AddSlot(string slot)
{
if (HasSlot(slot))
{
throw new InvalidOperationException($"Slot '{slot}' already exists.");
}
return slots[slot] = new InventorySlot(slot, this);
}
public void RemoveSlot(string slot)
{
if (!HasSlot(slot))
{
throw new InvalidOperationException($"Slow '{slot}' does not exist.");
}
if (Get(slot) != null && !Drop(slot))
{
// TODO: Handle this potential failiure better.
throw new InvalidOperationException("Unable to remove slot as the contained item could not be dropped");
}
slots.Remove(slot);
}
public bool HasSlot(string slot)
{
return slots.ContainsKey(slot);
}
private class InventorySlot : IInventorySlot
{
public IItemComponent Item { get; set; }
public string Name { get; }
public IInventoryComponent Owner { get; }
public InventorySlot(string name, IInventoryComponent owner)
{
Name = name;
Owner = owner;
}
}
}
}

View File

@@ -5,37 +5,21 @@ using SS14.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects
{
public class ItemComponent : StoreableComponent, IItemComponent, EntitySystems.IAttackHand
public class ItemComponent : StoreableComponent, EntitySystems.IAttackHand
{
public override string Name => "Item";
/// <inheritdoc />
public IInventorySlot ContainingSlot { get; private set; }
public void RemovedFromSlot()
{
if (ContainingSlot == null)
{
throw new InvalidOperationException("Item is not in a slot.");
}
ContainingSlot = null;
foreach (var component in Owner.GetComponents<ISpriteRenderableComponent>())
{
component.Visible = true;
}
}
public void EquippedToSlot(IInventorySlot slot)
public void EquippedToSlot(ContainerSlot slot)
{
if (ContainingSlot != null)
{
throw new InvalidOperationException("Item is already in a slot.");
}
ContainingSlot = slot;
foreach (var component in Owner.GetComponents<ISpriteRenderableComponent>())
{
component.Visible = false;
@@ -44,10 +28,6 @@ namespace Content.Server.GameObjects
public bool Attackhand(IEntity user)
{
if (ContainingSlot != null)
{
return false;
}
var hands = user.GetComponent<IHandsComponent>();
hands.PutInHand(this, hands.ActiveIndex, fallback: false);
return true;

View File

@@ -28,7 +28,7 @@ namespace Content.Server.GameObjects
{
base.OnAdd();
storage = Container.Create("storagebase", Owner);
storage = ContainerManagerComponent.Create<Container>("storagebase", Owner);
}
public override void ExposeData(EntitySerializer serializer)
@@ -105,7 +105,7 @@ namespace Content.Server.GameObjects
else
{
//Return the object to the hand since its too big or something like that
hands.PutInHand(attackwith.GetComponent<IItemComponent>());
hands.PutInHand(attackwith.GetComponent<ItemComponent>());
}
}
return false;
@@ -167,7 +167,7 @@ namespace Content.Server.GameObjects
Remove(entity);
UpdateClientInventory();
var item = entity.GetComponent<IItemComponent>();
var item = entity.GetComponent<ItemComponent>();
if (item != null && playerentity.TryGetComponent(out HandsComponent hands))
{
if (hands.PutInHand(item))

View File

@@ -30,6 +30,7 @@ namespace Content.Server.GameObjects
Wallet = 4,
Pocket = 12,
Box = 24,
Belt = 30,
Toolbox = 60,
Backpack = 100,
NoStoring = 9999

View File

@@ -0,0 +1,51 @@
using SS14.Server.GameObjects.Components.Container;
using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Content.Server.GameObjects
{
public class ContainerSlot : BaseContainer
{
public IEntity ContainedEntity { get; private set; } = null;
/// <inheritdoc />
public override IReadOnlyCollection<IEntity> ContainedEntities => new List<IEntity> { ContainedEntity }.AsReadOnly();
public ContainerSlot(string id, IContainerManager manager) : base(id, manager)
{
}
/// <inheritdoc />
public override bool CanInsert(IEntity toinsert)
{
if (ContainedEntity != null)
return false;
return base.CanInsert(toinsert);
}
/// <inheritdoc />
public override bool Contains(IEntity contained)
{
if (contained == ContainedEntity)
return true;
return false;
}
/// <inheritdoc />
protected override void InternalInsert(IEntity toinsert)
{
ContainedEntity = toinsert;
}
/// <inheritdoc />
protected override void InternalRemove(IEntity toremove)
{
ContainedEntity = null;
}
}
}