Convert StomachBehavior to a component/system + rejig body namespaces (#5249)

* Convert StomachBehavior to a component/system + rejig body namespaces

* test

* slightly more namespace changes

* remove

* Hello?????

* fuck you github test runner

* reviews

* oobsy!
This commit is contained in:
mirrorcult
2021-11-11 16:10:57 -07:00
committed by GitHub
parent 509e1ba6e7
commit 457e8c64ee
77 changed files with 326 additions and 307 deletions

View File

@@ -0,0 +1,18 @@
using Content.Shared.Chemistry.Components;
using Robust.Shared.GameObjects;
namespace Content.Shared.Body.Components
{
public abstract class SharedBloodstreamComponent : Component
{
/// <summary>
/// Attempts to transfer the provided solution to an internal solution.
/// Only supports complete transfers.
/// </summary>
/// <param name="solution">The solution to be transferred.</param>
/// <returns>Whether or not transfer was successful.</returns>
public abstract bool TryTransferSolution(Solution solution);
public const string DefaultSolutionName = "bloodstream";
}
}

View File

@@ -5,9 +5,7 @@ using System.Linq;
using Content.Shared.Body.Behavior;
using Content.Shared.Body.Part;
using Content.Shared.Body.Part.Property;
using Content.Shared.Body.Preset;
using Content.Shared.Body.Slot;
using Content.Shared.Body.Template;
using Content.Shared.Body.Prototypes;
using Content.Shared.CharacterAppearance.Systems;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;

View File

@@ -0,0 +1,418 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Body.Behavior;
using Content.Shared.Body.Part;
using Content.Shared.Body.Part.Property;
using Content.Shared.Body.Surgery;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Body.Components
{
[NetworkedComponent()]
public abstract class SharedBodyPartComponent : Component, IBodyPartContainer
{
public override string Name => "BodyPart";
private SharedBodyComponent? _body;
// TODO BODY Remove
[DataField("mechanisms")]
private readonly List<string> _mechanismIds = new();
public IReadOnlyList<string> MechanismIds => _mechanismIds;
[ViewVariables]
private readonly HashSet<SharedMechanismComponent> _mechanisms = new();
[ViewVariables]
public SharedBodyComponent? Body
{
get => _body;
set
{
if (_body == value)
{
return;
}
var old = _body;
_body = value;
if (old != null)
{
RemovedFromBody(old);
}
if (value != null)
{
AddedToBody(value);
}
}
}
/// <summary>
/// The string to show when displaying this part's name to players.
/// </summary>
[ViewVariables]
public string DisplayName => Name;
/// <summary>
/// <see cref="BodyPartType"/> that this <see cref="IBodyPart"/> is considered
/// to be.
/// For example, <see cref="BodyPartType.Arm"/>.
/// </summary>
[ViewVariables]
[DataField("partType")]
public BodyPartType PartType { get; private set; } = BodyPartType.Other;
/// <summary>
/// Determines how many mechanisms can be fit inside this
/// <see cref="SharedBodyPartComponent"/>.
/// </summary>
[ViewVariables] [DataField("size")] public int Size { get; private set; } = 1;
[ViewVariables] public int SizeUsed { get; private set; }
// TODO BODY size used
// TODO BODY surgerydata
/// <summary>
/// What types of BodyParts this <see cref="SharedBodyPartComponent"/> can easily attach to.
/// For the most part, most limbs aren't universal and require extra work to
/// attach between types.
/// </summary>
[ViewVariables]
[DataField("compatibility")]
public BodyPartCompatibility Compatibility { get; private set; } = BodyPartCompatibility.Universal;
// TODO BODY Mechanisms occupying different parts at the body level
[ViewVariables]
public IReadOnlyCollection<SharedMechanismComponent> Mechanisms => _mechanisms;
// TODO BODY Replace with a simulation of organs
/// <summary>
/// Whether or not the owning <see cref="Body"/> will die if all
/// <see cref="SharedBodyPartComponent"/>s of this type are removed from it.
/// </summary>
[ViewVariables]
[DataField("vital")]
public bool IsVital { get; private set; } = false;
[ViewVariables]
[DataField("symmetry")]
public BodyPartSymmetry Symmetry { get; private set; } = BodyPartSymmetry.None;
[ViewVariables]
public ISurgeryData? SurgeryDataComponent => Owner.GetComponentOrNull<ISurgeryData>();
protected virtual void OnAddMechanism(SharedMechanismComponent mechanism)
{
var prototypeId = mechanism.Owner.Prototype!.ID;
if (!_mechanismIds.Contains(prototypeId))
{
_mechanismIds.Add(prototypeId);
}
mechanism.Part = this;
SizeUsed += mechanism.Size;
Dirty();
}
protected virtual void OnRemoveMechanism(SharedMechanismComponent mechanism)
{
_mechanismIds.Remove(mechanism.Owner.Prototype!.ID);
mechanism.Part = null;
SizeUsed -= mechanism.Size;
Dirty();
}
public override ComponentState GetComponentState(ICommonSession player)
{
var mechanismIds = new EntityUid[_mechanisms.Count];
var i = 0;
foreach (var mechanism in _mechanisms)
{
mechanismIds[i] = mechanism.OwnerUid;
i++;
}
return new BodyPartComponentState(mechanismIds);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not BodyPartComponentState state)
{
return;
}
var newMechanisms = state.Mechanisms();
foreach (var mechanism in _mechanisms.ToArray())
{
if (!newMechanisms.Contains(mechanism))
{
RemoveMechanism(mechanism);
}
}
foreach (var mechanism in newMechanisms)
{
if (!_mechanisms.Contains(mechanism))
{
TryAddMechanism(mechanism, true);
}
}
}
public bool SurgeryCheck(SurgeryType surgery)
{
return SurgeryDataComponent?.CheckSurgery(surgery) ?? false;
}
public bool AttemptSurgery(SurgeryType toolType, IBodyPartContainer target, ISurgeon surgeon, IEntity performer)
{
DebugTools.AssertNotNull(toolType);
DebugTools.AssertNotNull(target);
DebugTools.AssertNotNull(surgeon);
DebugTools.AssertNotNull(performer);
return SurgeryDataComponent?.PerformSurgery(toolType, target, surgeon, performer) ?? false;
}
public bool CanAttachPart(SharedBodyPartComponent part)
{
DebugTools.AssertNotNull(part);
return SurgeryDataComponent?.CanAttachBodyPart(part) ?? false;
}
public virtual bool CanAddMechanism(SharedMechanismComponent mechanism)
{
DebugTools.AssertNotNull(mechanism);
return SurgeryDataComponent != null &&
SizeUsed + mechanism.Size <= Size &&
SurgeryDataComponent.CanAddMechanism(mechanism);
}
/// <summary>
/// Tries to add a <see cref="SharedMechanismComponent"/> to this part.
/// </summary>
/// <param name="mechanism">The mechanism to add.</param>
/// <param name="force">
/// Whether or not to check if the mechanism is compatible.
/// Passing true does not guarantee it to be added, for example if
/// it was already added before.
/// </param>
/// <returns>true if added, false otherwise even if it was already added.</returns>
public bool TryAddMechanism(SharedMechanismComponent mechanism, bool force = false)
{
DebugTools.AssertNotNull(mechanism);
if (!force && !CanAddMechanism(mechanism))
{
return false;
}
if (!_mechanisms.Add(mechanism))
{
return false;
}
OnAddMechanism(mechanism);
return true;
}
/// <summary>
/// Tries to remove the given <see cref="mechanism"/> from this part.
/// </summary>
/// <param name="mechanism">The mechanism to remove.</param>
/// <returns>True if it was removed, false otherwise.</returns>
public bool RemoveMechanism(SharedMechanismComponent mechanism)
{
DebugTools.AssertNotNull(mechanism);
if (!_mechanisms.Remove(mechanism))
{
return false;
}
OnRemoveMechanism(mechanism);
return true;
}
/// <summary>
/// Tries to remove the given <see cref="mechanism"/> from this
/// part and drops it at the specified coordinates.
/// </summary>
/// <param name="mechanism">The mechanism to remove.</param>
/// <param name="coordinates">The coordinates to drop it at.</param>
/// <returns>True if it was removed, false otherwise.</returns>
public bool RemoveMechanism(SharedMechanismComponent mechanism, EntityCoordinates coordinates)
{
if (RemoveMechanism(mechanism))
{
mechanism.Owner.Transform.Coordinates = coordinates;
return true;
}
return false;
}
/// <summary>
/// Tries to destroy the given <see cref="SharedMechanismComponent"/> from
/// this part.
/// The mechanism won't be deleted if it is not in this body part.
/// </summary>
/// <returns>
/// True if the mechanism was in this body part and destroyed,
/// false otherwise.
/// </returns>
public bool DeleteMechanism(SharedMechanismComponent mechanism)
{
DebugTools.AssertNotNull(mechanism);
if (!RemoveMechanism(mechanism))
{
return false;
}
mechanism.Owner.Delete();
return true;
}
private void AddedToBody(SharedBodyComponent body)
{
Owner.Transform.LocalRotation = 0;
Owner.Transform.AttachParent(body.Owner);
OnAddedToBody(body);
foreach (var mechanism in _mechanisms)
{
mechanism.AddedToBody(body);
}
}
private void RemovedFromBody(SharedBodyComponent old)
{
if (!Owner.Transform.Deleted)
{
Owner.Transform.AttachToGridOrMap();
}
OnRemovedFromBody(old);
foreach (var mechanism in _mechanisms)
{
mechanism.RemovedFromBody(old);
}
}
protected virtual void OnAddedToBody(SharedBodyComponent body) { }
protected virtual void OnRemovedFromBody(SharedBodyComponent old) { }
/// <summary>
/// Gibs the body part.
/// </summary>
public virtual void Gib()
{
foreach (var mechanism in _mechanisms)
{
RemoveMechanism(mechanism);
}
}
public bool HasProperty(Type type)
{
return Owner.HasComponent(type);
}
public bool HasProperty<T>() where T : class, IBodyPartProperty
{
return HasProperty(typeof(T));
}
public bool TryGetProperty(Type type,
[NotNullWhen(true)] out IBodyPartProperty? property)
{
if (!Owner.TryGetComponent(type, out var component))
{
property = null;
return false;
}
return (property = component as IBodyPartProperty) != null;
}
public bool TryGetProperty<T>([NotNullWhen(true)] out T? property) where T : class, IBodyPartProperty
{
return Owner.TryGetComponent(out property);
}
public bool HasMechanismBehavior<T>() where T : SharedMechanismBehavior
{
return Mechanisms.Any(m => m.HasBehavior<T>());
}
}
[Serializable, NetSerializable]
public class BodyPartComponentState : ComponentState
{
[NonSerialized] private List<SharedMechanismComponent>? _mechanisms;
public readonly EntityUid[] MechanismIds;
public BodyPartComponentState(EntityUid[] mechanismIds)
{
MechanismIds = mechanismIds;
}
public List<SharedMechanismComponent> Mechanisms(IEntityManager? entityManager = null)
{
if (_mechanisms != null)
{
return _mechanisms;
}
entityManager ??= IoCManager.Resolve<IEntityManager>();
var mechanisms = new List<SharedMechanismComponent>(MechanismIds.Length);
foreach (var id in MechanismIds)
{
if (!entityManager.TryGetEntity(id, out var entity))
{
continue;
}
if (!entity.TryGetComponent(out SharedMechanismComponent? mechanism))
{
continue;
}
mechanisms.Add(mechanism);
}
return _mechanisms = mechanisms;
}
}
}

View File

@@ -0,0 +1,28 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Body.Components
{
public abstract class SharedBodyScannerComponent : Component
{
public override string Name => "BodyScanner";
}
[Serializable, NetSerializable]
public enum BodyScannerUiKey
{
Key
}
[Serializable, NetSerializable]
public class BodyScannerUIState : BoundUserInterfaceState
{
public readonly EntityUid Uid;
public BodyScannerUIState(EntityUid uid)
{
Uid = uid;
}
}
}

View File

@@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Body.Behavior;
using Content.Shared.Body.Part;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Body.Components
{
public abstract class SharedMechanismComponent : Component, ISerializationHooks
{
public override string Name => "Mechanism";
protected readonly Dictionary<int, object> OptionsCache = new();
protected SharedBodyComponent? BodyCache;
protected int IdHash;
protected IEntity? PerformerCache;
private SharedBodyPartComponent? _part;
[DataField("behaviors", serverOnly: true)] private HashSet<SharedMechanismBehavior> _behaviorTypes = new();
private readonly Dictionary<Type, SharedMechanismBehavior> _behaviors = new();
public SharedBodyComponent? Body => Part?.Body;
public SharedBodyPartComponent? Part
{
get => _part;
set
{
if (_part == value)
{
return;
}
var old = _part;
_part = value;
if (old != null)
{
if (old.Body == null)
{
RemovedFromPart(old);
}
else
{
RemovedFromPartInBody(old.Body, old);
}
}
if (value != null)
{
if (value.Body == null)
{
AddedToPart(value);
}
else
{
AddedToPartInBody(value.Body, value);
}
}
}
}
public IReadOnlyDictionary<Type, SharedMechanismBehavior> Behaviors => _behaviors;
[DataField("maxDurability")] public int MaxDurability { get; set; } = 10;
[DataField("currentDurability")] public int CurrentDurability { get; set; } = 10;
[DataField("destroyThreshold")] public int DestroyThreshold { get; set; } = -10;
// TODO BODY: Surgery description and adding a message to the examine tooltip of the entity that owns this mechanism
// TODO BODY
[DataField("resistance")] public int Resistance { get; set; } = 0;
// TODO BODY OnSizeChanged
/// <summary>
/// Determines whether this
/// <see cref="SharedMechanismComponent"/> can fit into a <see cref="SharedBodyPartComponent"/>.
/// </summary>
[DataField("size")] public int Size { get; set; } = 1;
/// <summary>
/// What kind of <see cref="SharedBodyPartComponent"/> this
/// <see cref="SharedMechanismComponent"/> can be easily installed into.
/// </summary>
[DataField("compatibility")]
public BodyPartCompatibility Compatibility { get; set; } = BodyPartCompatibility.Universal;
void ISerializationHooks.BeforeSerialization()
{
_behaviorTypes = _behaviors.Values.ToHashSet();
}
void ISerializationHooks.AfterDeserialization()
{
foreach (var behavior in _behaviorTypes)
{
var type = behavior.GetType();
if (!_behaviors.TryAdd(type, behavior))
{
Logger.Warning($"Duplicate behavior in {nameof(SharedMechanismComponent)}: {type}.");
continue;
}
IoCManager.InjectDependencies(behavior);
}
}
protected override void Initialize()
{
base.Initialize();
foreach (var behavior in _behaviors.Values)
{
behavior.Initialize(this);
}
}
protected override void Startup()
{
base.Startup();
foreach (var behavior in _behaviors.Values)
{
behavior.Startup();
}
}
public bool EnsureBehavior<T>(out T behavior) where T : SharedMechanismBehavior, new()
{
if (_behaviors.TryGetValue(typeof(T), out var rawBehavior))
{
behavior = (T) rawBehavior;
return true;
}
behavior = IoCManager.Resolve<IDynamicTypeFactory>().CreateInstance<T>();
_behaviors.Add(typeof(T), behavior);
behavior.Initialize(this);
behavior.Startup();
return false;
}
public bool HasBehavior<T>() where T : SharedMechanismBehavior
{
return _behaviors.ContainsKey(typeof(T));
}
public bool TryRemoveBehavior<T>() where T : SharedMechanismBehavior
{
return _behaviors.Remove(typeof(T));
}
public void Update(float frameTime)
{
foreach (var behavior in _behaviors.Values)
{
behavior.Update(frameTime);
}
}
// TODO BODY Turn these into event listeners so they dont need to be exposed
public void AddedToBody(SharedBodyComponent body)
{
DebugTools.AssertNotNull(Body);
DebugTools.AssertNotNull(body);
foreach (var behavior in _behaviors.Values)
{
behavior.AddedToBody(body);
}
}
public void AddedToPart(SharedBodyPartComponent part)
{
DebugTools.AssertNotNull(Part);
DebugTools.AssertNotNull(part);
Owner.Transform.AttachParent(part.Owner);
foreach (var behavior in _behaviors.Values)
{
behavior.AddedToPart(part);
}
}
public void AddedToPartInBody(SharedBodyComponent body, SharedBodyPartComponent part)
{
DebugTools.AssertNotNull(Body);
DebugTools.AssertNotNull(body);
DebugTools.AssertNotNull(Part);
DebugTools.AssertNotNull(part);
Owner.Transform.AttachParent(part.Owner);
foreach (var behavior in _behaviors.Values)
{
behavior.AddedToPartInBody(body, part);
}
}
public void RemovedFromBody(SharedBodyComponent old)
{
DebugTools.AssertNull(Body);
DebugTools.AssertNotNull(old);
foreach (var behavior in _behaviors.Values)
{
behavior.RemovedFromBody(old);
}
}
public void RemovedFromPart(SharedBodyPartComponent old)
{
DebugTools.AssertNull(Part);
DebugTools.AssertNotNull(old);
Owner.Transform.AttachToGridOrMap();
foreach (var behavior in _behaviors.Values)
{
behavior.RemovedFromPart(old);
}
}
public void RemovedFromPartInBody(SharedBodyComponent oldBody, SharedBodyPartComponent oldPart)
{
DebugTools.AssertNull(Body);
DebugTools.AssertNotNull(oldBody);
DebugTools.AssertNull(Part);
DebugTools.AssertNotNull(oldPart);
Owner.Transform.AttachToGridOrMap();
foreach (var behavior in _behaviors.Values)
{
behavior.RemovedFromPartInBody(oldBody, oldPart);
}
}
}
}