using System; using System.Collections.Generic; using Robust.Shared.Interfaces.Serialization; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; using YamlDotNet.RepresentationModel; namespace Content.Shared.BodySystem { /// /// This class is a data capsule representing the standard format of a body. For instance, the "humanoid" BodyTemplate /// defines two arms, each connected to a torso and so on. Capable of loading data from a BodyTemplatePrototype. /// public class BodyTemplate { private int _hash; private string _name; private string _centerSlot; private Dictionary _slots = new Dictionary(); private Dictionary> _connections = new Dictionary>(); [ViewVariables] public int Hash => _hash; [ViewVariables] public string Name => _name; /// /// The name of the center BodyPart. For humans, this is set to "torso". Used in many calculations. /// [ViewVariables] public string CenterSlot => _centerSlot; /// /// Maps all parts on this template to its BodyPartType. For instance, "right arm" is mapped to "BodyPartType.arm" on the humanoid template. /// [ViewVariables] public Dictionary Slots => _slots; /// /// Maps limb name to the list of their connections to other limbs. For instance, on the humanoid template "torso" is mapped to a list containing "right arm", "left arm", /// "left leg", and "right leg". Only one of the limbs in a connection has to map it, i.e. humanoid template chooses to map "head" to "torso" and not the other way around. /// [ViewVariables] public Dictionary> Connections => _connections; public BodyTemplate() { _name = "empty"; } public BodyTemplate(BodyTemplatePrototype data) { LoadFromPrototype(data); } /// /// Somewhat costly operation. Stores an integer unique to this exact BodyTemplate in _hash when called. /// private void CacheHashCode() { int hash = 0; foreach (var(key, value) in _slots) { hash = HashCode.Combine(hash, key.GetHashCode()); } foreach (var (key, value) in _connections) { hash = HashCode.Combine(hash, key.GetHashCode()); foreach (var connection in value) { hash = HashCode.Combine(hash, connection.GetHashCode()); } } _hash = hash; } public virtual void LoadFromPrototype(BodyTemplatePrototype data) { _name = data.Name; _centerSlot = data.CenterSlot; _slots = data.Slots; _connections = data.Connections; CacheHashCode(); } } }