Separate part management from the rest of body manager component (#2017)
* Separate part management from the rest of body manager component * Component reference * Move more methods over * Fix docs and move over BodyPreset * Fix up body preset * Create initialize method and remove constructor for BodyPreset * Do the same for BodyTemplate and add Initialized properties * Fix BodyTemplate HashCode test * BodyTemplate test PLS
This commit is contained in:
@@ -93,7 +93,7 @@ namespace Content.Server.Body
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
body.DisconnectBodyPart(hand.Value, true);
|
body.RemovePart(hand.Value, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Content.Shared.Body.Part;
|
using Content.Shared.Body.Part;
|
||||||
using Content.Shared.Body.Preset;
|
using Content.Shared.Body.Preset;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
using Robust.Shared.ViewVariables;
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Server.Body
|
namespace Content.Server.Body
|
||||||
@@ -13,24 +14,25 @@ namespace Content.Server.Body
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class BodyPreset
|
public class BodyPreset
|
||||||
{
|
{
|
||||||
public BodyPreset(BodyPresetPrototype data)
|
[ViewVariables] public bool Initialized { get; private set; }
|
||||||
{
|
|
||||||
LoadFromPrototype(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
[ViewVariables] public string Name { get; private set; }
|
[ViewVariables] public string Name { get; protected set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps a template slot to the ID of the <see cref="IBodyPart"/> that should
|
/// Maps a template slot to the ID of the <see cref="IBodyPart"/>
|
||||||
/// fill it. E.g. "right arm" : "BodyPart.arm.basic_human".
|
/// that should fill it. E.g. "right arm" : "BodyPart.arm.basic_human".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public Dictionary<string, string> PartIDs { get; private set; }
|
public Dictionary<string, string> PartIDs { get; protected set; }
|
||||||
|
|
||||||
protected virtual void LoadFromPrototype(BodyPresetPrototype data)
|
public virtual void Initialize(BodyPresetPrototype prototype)
|
||||||
{
|
{
|
||||||
Name = data.Name;
|
DebugTools.Assert(!Initialized, $"{nameof(BodyPreset)} {Name} has already been initialized!");
|
||||||
PartIDs = data.PartIDs;
|
|
||||||
|
Name = prototype.Name;
|
||||||
|
PartIDs = prototype.PartIDs;
|
||||||
|
|
||||||
|
Initialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using Content.Server.GameObjects.Components.Body;
|
using Content.Server.GameObjects.Components.Body;
|
||||||
using Content.Shared.Body.Template;
|
using Content.Shared.Body.Template;
|
||||||
using Content.Shared.GameObjects.Components.Body;
|
using Content.Shared.GameObjects.Components.Body;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
using Robust.Shared.ViewVariables;
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Server.Body
|
namespace Content.Server.Body
|
||||||
@@ -11,35 +12,22 @@ namespace Content.Server.Body
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class is a data capsule representing the standard format of a
|
/// This class is a data capsule representing the standard format of a
|
||||||
/// <see cref="BodyManagerComponent"/>.
|
/// <see cref="BodyManagerComponent"/>.
|
||||||
/// For instance, the "humanoid" BodyTemplate defines two arms, each connected to
|
/// For instance, the "humanoid" BodyTemplate defines two arms, each
|
||||||
/// a torso and so on.
|
/// connected to a torso and so on.
|
||||||
/// Capable of loading data from a <see cref="BodyTemplatePrototype"/>.
|
/// Capable of loading data from a <see cref="BodyTemplatePrototype"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BodyTemplate
|
public class BodyTemplate
|
||||||
{
|
{
|
||||||
public BodyTemplate()
|
[ViewVariables] public bool Initialized { get; private set; }
|
||||||
{
|
|
||||||
Name = "empty";
|
|
||||||
CenterSlot = "";
|
|
||||||
Slots = new Dictionary<string, BodyPartType>();
|
|
||||||
Connections = new Dictionary<string, List<string>>();
|
|
||||||
Layers = new Dictionary<string, string>();
|
|
||||||
MechanismLayers = new Dictionary<string, string>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public BodyTemplate(BodyTemplatePrototype data)
|
[ViewVariables] public string Name { get; private set; } = "";
|
||||||
{
|
|
||||||
LoadFromPrototype(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
[ViewVariables] public string Name { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the center BodyPart. For humans, this is set to "torso".
|
/// The name of the center BodyPart. For humans, this is set to "torso".
|
||||||
/// Used in many calculations.
|
/// Used in many calculations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public string CenterSlot { get; set; }
|
public string CenterSlot { get; set; } = "";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps all parts on this template to its BodyPartType.
|
/// Maps all parts on this template to its BodyPartType.
|
||||||
@@ -47,7 +35,7 @@ namespace Content.Server.Body
|
|||||||
/// template.
|
/// template.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public Dictionary<string, BodyPartType> Slots { get; private set; }
|
public Dictionary<string, BodyPartType> Slots { get; private set; } = new Dictionary<string, BodyPartType>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps limb name to the list of their connections to other limbs.
|
/// Maps limb name to the list of their connections to other limbs.
|
||||||
@@ -58,13 +46,13 @@ namespace Content.Server.Body
|
|||||||
/// map "left arm" to "torso".
|
/// map "left arm" to "torso".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public Dictionary<string, List<string>> Connections { get; private set; }
|
public Dictionary<string, List<string>> Connections { get; private set; } = new Dictionary<string, List<string>>();
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public Dictionary<string, string> Layers { get; private set; }
|
public Dictionary<string, string> Layers { get; private set; } = new Dictionary<string, string>();
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public Dictionary<string, string> MechanismLayers { get; private set; }
|
public Dictionary<string, string> MechanismLayers { get; private set; } = new Dictionary<string, string>();
|
||||||
|
|
||||||
public bool Equals(BodyTemplate other)
|
public bool Equals(BodyTemplate other)
|
||||||
{
|
{
|
||||||
@@ -75,7 +63,7 @@ namespace Content.Server.Body
|
|||||||
/// Checks if the given slot exists in this <see cref="BodyTemplate"/>.
|
/// Checks if the given slot exists in this <see cref="BodyTemplate"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>True if it does, false otherwise.</returns>
|
/// <returns>True if it does, false otherwise.</returns>
|
||||||
public bool SlotExists(string slotName)
|
public bool HasSlot(string slotName)
|
||||||
{
|
{
|
||||||
return Slots.Keys.Any(slot => slot == slotName);
|
return Slots.Keys.Any(slot => slot == slotName);
|
||||||
}
|
}
|
||||||
@@ -132,14 +120,18 @@ namespace Content.Server.Body
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void LoadFromPrototype(BodyTemplatePrototype data)
|
public virtual void Initialize(BodyTemplatePrototype prototype)
|
||||||
{
|
{
|
||||||
Name = data.Name;
|
DebugTools.Assert(!Initialized, $"{nameof(BodyTemplate)} {Name} has already been initialized!");
|
||||||
CenterSlot = data.CenterSlot;
|
|
||||||
Slots = data.Slots;
|
Name = prototype.Name;
|
||||||
Connections = data.Connections;
|
CenterSlot = prototype.CenterSlot;
|
||||||
Layers = data.Layers;
|
Slots = new Dictionary<string, BodyPartType>(prototype.Slots);
|
||||||
MechanismLayers = data.MechanismLayers;
|
Connections = new Dictionary<string, List<string>>(prototype.Connections);
|
||||||
|
Layers = new Dictionary<string, string>(prototype.Layers);
|
||||||
|
MechanismLayers = new Dictionary<string, string>(prototype.MechanismLayers);
|
||||||
|
|
||||||
|
Initialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,9 +31,16 @@ namespace Content.Server.Body.Network
|
|||||||
public virtual void OnRemove() { }
|
public virtual void OnRemove() { }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called every update by <see cref="BodyManagerComponent.Update"/>.
|
/// Called every update by
|
||||||
|
/// <see cref="BodyManagerComponent.PreMetabolism"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void Update(float frameTime) { }
|
public virtual void PreMetabolism(float frameTime) { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called every update by
|
||||||
|
/// <see cref="BodyManagerComponent.PostMetabolism"/>.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void PostMetabolism(float frameTime) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BodyNetworkExtensions
|
public static class BodyNetworkExtensions
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ namespace Content.Server.Body.Surgery
|
|||||||
performer.PopupMessage(Loc.GetString("Saw off the limb!"));
|
performer.PopupMessage(Loc.GetString("Saw off the limb!"));
|
||||||
|
|
||||||
// TODO do_after: Delay
|
// TODO do_after: Delay
|
||||||
bmTarget.DisconnectBodyPart(Parent, true);
|
bmTarget.RemovePart(Parent, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,543 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Linq;
|
||||||
|
using Content.Server.Body;
|
||||||
|
using Content.Server.GameObjects.EntitySystems;
|
||||||
|
using Content.Server.Interfaces.GameObjects.Components.Interaction;
|
||||||
|
using Content.Shared.Body.Part.Properties.Movement;
|
||||||
|
using Content.Shared.Body.Part.Properties.Other;
|
||||||
|
using Content.Shared.GameObjects.Components.Body;
|
||||||
|
using Content.Shared.GameObjects.Components.Damage;
|
||||||
|
using Content.Shared.GameObjects.Components.Movement;
|
||||||
|
using Robust.Shared.GameObjects.Systems;
|
||||||
|
using Robust.Shared.Interfaces.GameObjects;
|
||||||
|
using Robust.Shared.Log;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
|
namespace Content.Server.GameObjects.Components.Body
|
||||||
|
{
|
||||||
|
public partial class BodyManagerComponent
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, IBodyPart> _parts = new Dictionary<string, IBodyPart>();
|
||||||
|
|
||||||
|
[ViewVariables] public BodyPreset Preset { get; private set; } = default!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// All <see cref="IBodyPart"></see> with <see cref="LegProperty"></see>
|
||||||
|
/// that are currently affecting move speed, mapped to how big that leg
|
||||||
|
/// they're on is.
|
||||||
|
/// </summary>
|
||||||
|
[ViewVariables]
|
||||||
|
private readonly Dictionary<IBodyPart, float> _activeLegs = new Dictionary<IBodyPart, float>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps <see cref="BodyTemplate"/> slot name to the <see cref="IBodyPart"/>
|
||||||
|
/// object filling it (if there is one).
|
||||||
|
/// </summary>
|
||||||
|
[ViewVariables]
|
||||||
|
public IReadOnlyDictionary<string, IBodyPart> Parts => _parts;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of all occupied slots in this body, taken from the values of
|
||||||
|
/// <see cref="Parts"/>.
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<string> OccupiedSlots => Parts.Keys;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of all slots in this body, taken from the keys of
|
||||||
|
/// <see cref="Template"/> slots.
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<string> AllSlots => Template.Slots.Keys;
|
||||||
|
|
||||||
|
public bool TryAddPart(string slot, DroppedBodyPartComponent part, bool force = false)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(part);
|
||||||
|
|
||||||
|
if (!TryAddPart(slot, part.ContainedBodyPart, force))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
part.Owner.Delete();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryAddPart(string slot, IBodyPart part, bool force = false)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(part);
|
||||||
|
DebugTools.AssertNotNull(slot);
|
||||||
|
|
||||||
|
// Make sure the given slot exists
|
||||||
|
if (!force)
|
||||||
|
{
|
||||||
|
if (!HasSlot(slot))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// And that nothing is in it
|
||||||
|
if (!_parts.TryAdd(slot, part))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_parts[slot] = part;
|
||||||
|
}
|
||||||
|
|
||||||
|
part.Body = this;
|
||||||
|
|
||||||
|
var argsAdded = new BodyPartAddedEventArgs(part, slot);
|
||||||
|
|
||||||
|
foreach (var component in Owner.GetAllComponents<IBodyPartAdded>().ToArray())
|
||||||
|
{
|
||||||
|
component.BodyPartAdded(argsAdded);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Sort this duplicate out
|
||||||
|
OnBodyChanged();
|
||||||
|
|
||||||
|
if (!Template.Layers.TryGetValue(slot, out var partMap) ||
|
||||||
|
!_reflectionManager.TryParseEnumReference(partMap, out var partEnum))
|
||||||
|
{
|
||||||
|
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {partMap} for body part {part.Name}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
part.RSIMap = partEnum;
|
||||||
|
|
||||||
|
var partMessage = new BodyPartAddedMessage(part.RSIPath, part.RSIState, partEnum);
|
||||||
|
|
||||||
|
SendNetworkMessage(partMessage);
|
||||||
|
|
||||||
|
foreach (var mechanism in part.Mechanisms)
|
||||||
|
{
|
||||||
|
if (!Template.MechanismLayers.TryGetValue(mechanism.Id, out var mechanismMap))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_reflectionManager.TryParseEnumReference(mechanismMap, out var mechanismEnum))
|
||||||
|
{
|
||||||
|
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {mechanismMap} for mechanism {mechanism.Id}.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mechanismMessage = new MechanismSpriteAddedMessage(mechanismEnum);
|
||||||
|
|
||||||
|
SendNetworkMessage(mechanismMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasPart(string slot)
|
||||||
|
{
|
||||||
|
return _parts.ContainsKey(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemovePart(IBodyPart part, bool drop)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(part);
|
||||||
|
|
||||||
|
var slotName = _parts.FirstOrDefault(x => x.Value == part).Key;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(slotName)) return;
|
||||||
|
|
||||||
|
RemovePart(slotName, drop);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemovePart(string slot, bool drop)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(slot);
|
||||||
|
|
||||||
|
if (!_parts.Remove(slot, out var part))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEntity? dropped = null;
|
||||||
|
if (drop)
|
||||||
|
{
|
||||||
|
part.SpawnDropped(out dropped);
|
||||||
|
}
|
||||||
|
|
||||||
|
part.Body = null;
|
||||||
|
|
||||||
|
var args = new BodyPartRemovedEventArgs(part, slot);
|
||||||
|
|
||||||
|
foreach (var component in Owner.GetAllComponents<IBodyPartRemoved>())
|
||||||
|
{
|
||||||
|
component.BodyPartRemoved(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (part.RSIMap != null)
|
||||||
|
{
|
||||||
|
var message = new BodyPartRemovedMessage(part.RSIMap, dropped?.Uid);
|
||||||
|
SendNetworkMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var mechanism in part.Mechanisms)
|
||||||
|
{
|
||||||
|
if (!Template.MechanismLayers.TryGetValue(mechanism.Id, out var mechanismMap))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_reflectionManager.TryParseEnumReference(mechanismMap, out var mechanismEnum))
|
||||||
|
{
|
||||||
|
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {mechanismMap} for mechanism {mechanism.Id}.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mechanismMessage = new MechanismSpriteRemovedMessage(mechanismEnum);
|
||||||
|
|
||||||
|
SendNetworkMessage(mechanismMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CurrentDamageState == DamageState.Dead) return true;
|
||||||
|
|
||||||
|
// creadth: fall down if no legs
|
||||||
|
if (part.PartType == BodyPartType.Leg && Parts.Count(x => x.Value.PartType == BodyPartType.Leg) == 0)
|
||||||
|
{
|
||||||
|
EntitySystem.Get<StandingStateSystem>().Down(Owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
// creadth: immediately kill entity if last vital part removed
|
||||||
|
if (part.IsVital && Parts.Count(x => x.Value.PartType == part.PartType) == 0)
|
||||||
|
{
|
||||||
|
CurrentDamageState = DamageState.Dead;
|
||||||
|
ForceHealthChangedEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TryGetSlotConnections(slot, out var connections))
|
||||||
|
{
|
||||||
|
foreach (var connectionName in connections)
|
||||||
|
{
|
||||||
|
if (TryGetPart(connectionName, out var result) && !ConnectedToCenter(result))
|
||||||
|
{
|
||||||
|
RemovePart(connectionName, drop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OnBodyChanged();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemovePart(IBodyPart part, [NotNullWhen(true)] out string? slot)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(part);
|
||||||
|
|
||||||
|
var pair = _parts.FirstOrDefault(kvPair => kvPair.Value == part);
|
||||||
|
|
||||||
|
if (pair.Equals(default))
|
||||||
|
{
|
||||||
|
slot = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
slot = pair.Key;
|
||||||
|
|
||||||
|
return RemovePart(slot, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEntity? DropPart(IBodyPart part)
|
||||||
|
{
|
||||||
|
DebugTools.AssertNotNull(part);
|
||||||
|
|
||||||
|
if (!_parts.ContainsValue(part))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!RemovePart(part, out var slotName))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call disconnect on all limbs that were hanging off this limb.
|
||||||
|
if (TryGetSlotConnections(slotName, out var connections))
|
||||||
|
{
|
||||||
|
// This loop is an unoptimized travesty. TODO: optimize to be less shit
|
||||||
|
foreach (var connectionName in connections)
|
||||||
|
{
|
||||||
|
if (TryGetPart(connectionName, out var result) && !ConnectedToCenter(result))
|
||||||
|
{
|
||||||
|
RemovePart(connectionName, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
part.SpawnDropped(out var dropped);
|
||||||
|
|
||||||
|
OnBodyChanged();
|
||||||
|
return dropped;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ConnectedToCenter(IBodyPart part)
|
||||||
|
{
|
||||||
|
var searchedSlots = new List<string>();
|
||||||
|
|
||||||
|
return TryGetSlot(part, out var result) &&
|
||||||
|
ConnectedToCenterPartRecursion(searchedSlots, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ConnectedToCenterPartRecursion(ICollection<string> searchedSlots, string slotName)
|
||||||
|
{
|
||||||
|
if (!TryGetPart(slotName, out var part))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (part == CenterPart())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
searchedSlots.Add(slotName);
|
||||||
|
|
||||||
|
if (!TryGetSlotConnections(slotName, out var connections))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var connection in connections)
|
||||||
|
{
|
||||||
|
if (!searchedSlots.Contains(connection) &&
|
||||||
|
ConnectedToCenterPartRecursion(searchedSlots, connection))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IBodyPart? CenterPart()
|
||||||
|
{
|
||||||
|
Parts.TryGetValue(Template.CenterSlot, out var center);
|
||||||
|
return center;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasSlot(string slot)
|
||||||
|
{
|
||||||
|
return Template.HasSlot(slot);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetPart(string slot, [NotNullWhen(true)] out IBodyPart? result)
|
||||||
|
{
|
||||||
|
return Parts.TryGetValue(slot, out result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetSlot(IBodyPart part, [NotNullWhen(true)] out string? slot)
|
||||||
|
{
|
||||||
|
// We enforce that there is only one of each value in the dictionary,
|
||||||
|
// so we can iterate through the dictionary values to get the key from there.
|
||||||
|
var pair = Parts.FirstOrDefault(x => x.Value == part);
|
||||||
|
slot = pair.Key;
|
||||||
|
|
||||||
|
return !pair.Equals(default);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetSlotType(string slot, out BodyPartType result)
|
||||||
|
{
|
||||||
|
return Template.Slots.TryGetValue(slot, out result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetSlotConnections(string slot, [NotNullWhen(true)] out List<string>? connections)
|
||||||
|
{
|
||||||
|
return Template.Connections.TryGetValue(slot, out connections);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetPartConnections(string slot, [NotNullWhen(true)] out List<IBodyPart>? result)
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
|
||||||
|
if (!Template.Connections.TryGetValue(slot, out var connections))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var toReturn = new List<IBodyPart>();
|
||||||
|
foreach (var connection in connections)
|
||||||
|
{
|
||||||
|
if (TryGetPart(connection, out var partResult))
|
||||||
|
{
|
||||||
|
toReturn.Add(partResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toReturn.Count <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = toReturn;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetPartConnections(IBodyPart part, [NotNullWhen(true)] out List<IBodyPart>? connections)
|
||||||
|
{
|
||||||
|
connections = null;
|
||||||
|
|
||||||
|
return TryGetSlot(part, out var slotName) &&
|
||||||
|
TryGetPartConnections(slotName, out connections);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<IBodyPart> GetPartsOfType(BodyPartType type)
|
||||||
|
{
|
||||||
|
var toReturn = new List<IBodyPart>();
|
||||||
|
|
||||||
|
foreach (var part in Parts.Values)
|
||||||
|
{
|
||||||
|
if (part.PartType == type)
|
||||||
|
{
|
||||||
|
toReturn.Add(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CalculateSpeed()
|
||||||
|
{
|
||||||
|
if (!Owner.TryGetComponent(out MovementSpeedModifierComponent? playerMover))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float speedSum = 0;
|
||||||
|
foreach (var part in _activeLegs.Keys)
|
||||||
|
{
|
||||||
|
if (!part.HasProperty<LegProperty>())
|
||||||
|
{
|
||||||
|
_activeLegs.Remove(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (key, value) in _activeLegs)
|
||||||
|
{
|
||||||
|
if (key.TryGetProperty(out LegProperty? leg))
|
||||||
|
{
|
||||||
|
// Speed of a leg = base speed * (1+log1024(leg length))
|
||||||
|
speedSum += leg.Speed * (1 + (float) Math.Log(value, 1024.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (speedSum <= 0.001f || _activeLegs.Count <= 0)
|
||||||
|
{
|
||||||
|
playerMover.BaseWalkSpeed = 0.8f;
|
||||||
|
playerMover.BaseSprintSpeed = 2.0f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Extra legs stack diminishingly.
|
||||||
|
// Final speed = speed sum/(leg count-log4(leg count))
|
||||||
|
playerMover.BaseWalkSpeed =
|
||||||
|
speedSum / (_activeLegs.Count - (float) Math.Log(_activeLegs.Count, 4.0));
|
||||||
|
|
||||||
|
playerMover.BaseSprintSpeed = playerMover.BaseWalkSpeed * 1.75f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Called when the layout of this body changes.
|
||||||
|
/// </summary>
|
||||||
|
private void OnBodyChanged()
|
||||||
|
{
|
||||||
|
// Calculate move speed based on this body.
|
||||||
|
if (Owner.HasComponent<MovementSpeedModifierComponent>())
|
||||||
|
{
|
||||||
|
_activeLegs.Clear();
|
||||||
|
var legParts = Parts.Values.Where(x => x.HasProperty(typeof(LegProperty)));
|
||||||
|
|
||||||
|
foreach (var part in legParts)
|
||||||
|
{
|
||||||
|
var footDistance = DistanceToNearestFoot(part);
|
||||||
|
|
||||||
|
if (Math.Abs(footDistance - float.MinValue) > 0.001f)
|
||||||
|
{
|
||||||
|
_activeLegs.Add(part, footDistance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculateSpeed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the combined length of the distance to the nearest <see cref="BodyPart"/> with a
|
||||||
|
/// <see cref="FootProperty"/>. Returns <see cref="float.MinValue"/>
|
||||||
|
/// if there is no foot found. If you consider a <see cref="BodyManagerComponent"/> a node map, then it will look for
|
||||||
|
/// a foot node from the given node. It can
|
||||||
|
/// only search through BodyParts with <see cref="ExtensionProperty"/>.
|
||||||
|
/// </summary>
|
||||||
|
public float DistanceToNearestFoot(IBodyPart source)
|
||||||
|
{
|
||||||
|
if (source.HasProperty<FootProperty>() && source.TryGetProperty<ExtensionProperty>(out var property))
|
||||||
|
{
|
||||||
|
return property.ReachDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
return LookForFootRecursion(source, new List<BodyPart>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private float LookForFootRecursion(IBodyPart current,
|
||||||
|
ICollection<BodyPart> searchedParts)
|
||||||
|
{
|
||||||
|
if (!current.TryGetProperty<ExtensionProperty>(out var extProperty))
|
||||||
|
{
|
||||||
|
return float.MinValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all connected parts if the current part has an extension property
|
||||||
|
if (!TryGetPartConnections(current, out var connections))
|
||||||
|
{
|
||||||
|
return float.MinValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a connected BodyPart is a foot, return this BodyPart's length.
|
||||||
|
foreach (var connection in connections)
|
||||||
|
{
|
||||||
|
if (!searchedParts.Contains(connection) && connection.HasProperty<FootProperty>())
|
||||||
|
{
|
||||||
|
return extProperty.ReachDistance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, get the recursion values of all connected BodyParts and
|
||||||
|
// store them in a list.
|
||||||
|
var distances = new List<float>();
|
||||||
|
foreach (var connection in connections)
|
||||||
|
{
|
||||||
|
if (!searchedParts.Contains(connection))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = LookForFootRecursion(connection, searchedParts);
|
||||||
|
|
||||||
|
if (Math.Abs(result - float.MinValue) > 0.001f)
|
||||||
|
{
|
||||||
|
distances.Add(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If one or more of the searches found a foot, return the smallest one
|
||||||
|
// and add this ones length.
|
||||||
|
if (distances.Count > 0)
|
||||||
|
{
|
||||||
|
return distances.Min<float>() + extProperty.ReachDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
return float.MinValue;
|
||||||
|
|
||||||
|
// No extension property, no go.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,16 +2,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
|
||||||
using Content.Server.Body;
|
using Content.Server.Body;
|
||||||
using Content.Server.Body.Network;
|
using Content.Server.Body.Network;
|
||||||
using Content.Server.GameObjects.Components.Metabolism;
|
using Content.Server.GameObjects.Components.Metabolism;
|
||||||
using Content.Server.GameObjects.EntitySystems;
|
using Content.Server.GameObjects.EntitySystems;
|
||||||
using Content.Server.Interfaces.GameObjects.Components.Interaction;
|
|
||||||
using Content.Server.Observer;
|
using Content.Server.Observer;
|
||||||
using Content.Shared.Body.Part;
|
using Content.Shared.Body.Part;
|
||||||
using Content.Shared.Body.Part.Properties.Movement;
|
|
||||||
using Content.Shared.Body.Part.Properties.Other;
|
|
||||||
using Content.Shared.Body.Preset;
|
using Content.Shared.Body.Preset;
|
||||||
using Content.Shared.Body.Template;
|
using Content.Shared.Body.Template;
|
||||||
using Content.Shared.GameObjects.Components.Body;
|
using Content.Shared.GameObjects.Components.Body;
|
||||||
@@ -19,11 +15,8 @@ using Content.Shared.GameObjects.Components.Damage;
|
|||||||
using Content.Shared.GameObjects.Components.Movement;
|
using Content.Shared.GameObjects.Components.Movement;
|
||||||
using Robust.Server.Interfaces.Player;
|
using Robust.Server.Interfaces.Player;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.GameObjects.Systems;
|
|
||||||
using Robust.Shared.Interfaces.GameObjects;
|
|
||||||
using Robust.Shared.Interfaces.Reflection;
|
using Robust.Shared.Interfaces.Reflection;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
using Robust.Shared.Log;
|
|
||||||
using Robust.Shared.Players;
|
using Robust.Shared.Players;
|
||||||
using Robust.Shared.Prototypes;
|
using Robust.Shared.Prototypes;
|
||||||
using Robust.Shared.Serialization;
|
using Robust.Shared.Serialization;
|
||||||
@@ -39,8 +32,9 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
[RegisterComponent]
|
[RegisterComponent]
|
||||||
[ComponentReference(typeof(IDamageableComponent))]
|
[ComponentReference(typeof(IDamageableComponent))]
|
||||||
[ComponentReference(typeof(ISharedBodyManagerComponent))]
|
[ComponentReference(typeof(ISharedBodyManagerComponent))]
|
||||||
|
[ComponentReference(typeof(IBodyPartManager))]
|
||||||
[ComponentReference(typeof(IBodyManagerComponent))]
|
[ComponentReference(typeof(IBodyManagerComponent))]
|
||||||
public class BodyManagerComponent : SharedBodyManagerComponent, IBodyPartContainer, IRelayMoveInput, IBodyManagerComponent
|
public partial class BodyManagerComponent : SharedBodyManagerComponent, IBodyPartContainer, IRelayMoveInput, IBodyManagerComponent
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||||
[Dependency] private readonly IBodyNetworkFactory _bodyNetworkFactory = default!;
|
[Dependency] private readonly IBodyNetworkFactory _bodyNetworkFactory = default!;
|
||||||
@@ -48,41 +42,10 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
|
|
||||||
[ViewVariables] private string _presetName = default!;
|
[ViewVariables] private string _presetName = default!;
|
||||||
|
|
||||||
private readonly Dictionary<string, IBodyPart> _parts = new Dictionary<string, IBodyPart>();
|
|
||||||
|
|
||||||
[ViewVariables] private readonly Dictionary<Type, BodyNetwork> _networks = new Dictionary<Type, BodyNetwork>();
|
[ViewVariables] private readonly Dictionary<Type, BodyNetwork> _networks = new Dictionary<Type, BodyNetwork>();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// All <see cref="IBodyPart"></see> with <see cref="LegProperty"></see>
|
|
||||||
/// that are currently affecting move speed, mapped to how big that leg
|
|
||||||
/// they're on is.
|
|
||||||
/// </summary>
|
|
||||||
[ViewVariables]
|
|
||||||
private readonly Dictionary<IBodyPart, float> _activeLegs = new Dictionary<IBodyPart, float>();
|
|
||||||
|
|
||||||
[ViewVariables] public BodyTemplate Template { get; private set; } = default!;
|
[ViewVariables] public BodyTemplate Template { get; private set; } = default!;
|
||||||
|
|
||||||
[ViewVariables] public BodyPreset Preset { get; private set; } = default!;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Maps <see cref="BodyTemplate"/> slot name to the <see cref="IBodyPart"/>
|
|
||||||
/// object filling it (if there is one).
|
|
||||||
/// </summary>
|
|
||||||
[ViewVariables]
|
|
||||||
public IReadOnlyDictionary<string, IBodyPart> Parts => _parts;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// List of all slots in this body, taken from the keys of
|
|
||||||
/// <see cref="Template"/> slots.
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<string> AllSlots => Template.Slots.Keys;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// List of all occupied slots in this body, taken from the values of
|
|
||||||
/// <see cref="Parts"/>.
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<string> OccupiedSlots => Parts.Keys;
|
|
||||||
|
|
||||||
public override void ExposeData(ObjectSerializer serializer)
|
public override void ExposeData(ObjectSerializer serializer)
|
||||||
{
|
{
|
||||||
base.ExposeData(serializer);
|
base.ExposeData(serializer);
|
||||||
@@ -92,14 +55,15 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
"bodyTemplate.Humanoid",
|
"bodyTemplate.Humanoid",
|
||||||
template =>
|
template =>
|
||||||
{
|
{
|
||||||
if (!_prototypeManager.TryIndex(template, out BodyTemplatePrototype templateData))
|
if (!_prototypeManager.TryIndex(template, out BodyTemplatePrototype prototype))
|
||||||
{
|
{
|
||||||
// Invalid prototype
|
// Invalid prototype
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
$"No {nameof(BodyTemplatePrototype)} found with name {template}");
|
$"No {nameof(BodyTemplatePrototype)} found with name {template}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Template = new BodyTemplate(templateData);
|
Template = new BodyTemplate();
|
||||||
|
Template.Initialize(prototype);
|
||||||
},
|
},
|
||||||
() => Template.Name);
|
() => Template.Name);
|
||||||
|
|
||||||
@@ -108,14 +72,15 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
"bodyPreset.BasicHuman",
|
"bodyPreset.BasicHuman",
|
||||||
preset =>
|
preset =>
|
||||||
{
|
{
|
||||||
if (!_prototypeManager.TryIndex(preset, out BodyPresetPrototype presetData))
|
if (!_prototypeManager.TryIndex(preset, out BodyPresetPrototype prototype))
|
||||||
{
|
{
|
||||||
// Invalid prototype
|
// Invalid prototype
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
$"No {nameof(BodyPresetPrototype)} found with name {preset}");
|
$"No {nameof(BodyPresetPrototype)} found with name {preset}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Preset = new BodyPreset(presetData);
|
Preset = new BodyPreset();
|
||||||
|
Preset.Initialize(prototype);
|
||||||
},
|
},
|
||||||
() => _presetName);
|
() => _presetName);
|
||||||
}
|
}
|
||||||
@@ -156,7 +121,7 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try and remove an existing limb if that exists.
|
// Try and remove an existing limb if that exists.
|
||||||
RemoveBodyPart(slotName, false);
|
RemovePart(slotName, false);
|
||||||
|
|
||||||
// Add a new BodyPart with the BodyPartPrototype as a baseline to our
|
// Add a new BodyPart with the BodyPartPrototype as a baseline to our
|
||||||
// BodyComponent.
|
// BodyComponent.
|
||||||
@@ -167,21 +132,21 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
OnBodyChanged(); // TODO: Duplicate code
|
OnBodyChanged(); // TODO: Duplicate code
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
// /// <summary>
|
||||||
/// Changes the current <see cref="BodyTemplate"/> to the given
|
// /// Changes the current <see cref="BodyTemplate"/> to the given
|
||||||
/// <see cref="BodyTemplate"/>.
|
// /// <see cref="BodyTemplate"/>.
|
||||||
/// Attempts to keep previous <see cref="IBodyPart"/> if there is a
|
// /// Attempts to keep previous <see cref="IBodyPart"/> if there is a
|
||||||
/// slot for them in both <see cref="BodyTemplate"/>.
|
// /// slot for them in both <see cref="BodyTemplate"/>.
|
||||||
/// </summary>
|
// /// </summary>
|
||||||
public void ChangeBodyTemplate(BodyTemplatePrototype newTemplate)
|
// public void ChangeBodyTemplate(BodyTemplatePrototype newTemplate)
|
||||||
{
|
// {
|
||||||
foreach (var part in Parts)
|
// foreach (var part in Parts)
|
||||||
{
|
// {
|
||||||
// TODO: Make this work.
|
// // TODO: Make this work.
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
OnBodyChanged();
|
// OnBodyChanged();
|
||||||
}
|
// }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This method is called by <see cref="BodySystem.Update"/> before
|
/// This method is called by <see cref="BodySystem.Update"/> before
|
||||||
@@ -201,7 +166,7 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
|
|
||||||
foreach (var network in _networks.Values)
|
foreach (var network in _networks.Values)
|
||||||
{
|
{
|
||||||
network.Update(frameTime);
|
network.PreMetabolism(frameTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,73 +188,7 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
|
|
||||||
foreach (var network in _networks.Values)
|
foreach (var network in _networks.Values)
|
||||||
{
|
{
|
||||||
network.Update(frameTime);
|
network.PostMetabolism(frameTime);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when the layout of this body changes.
|
|
||||||
/// </summary>
|
|
||||||
private void OnBodyChanged()
|
|
||||||
{
|
|
||||||
// Calculate move speed based on this body.
|
|
||||||
if (Owner.HasComponent<MovementSpeedModifierComponent>())
|
|
||||||
{
|
|
||||||
_activeLegs.Clear();
|
|
||||||
var legParts = Parts.Values.Where(x => x.HasProperty(typeof(LegProperty)));
|
|
||||||
|
|
||||||
foreach (var part in legParts)
|
|
||||||
{
|
|
||||||
var footDistance = DistanceToNearestFoot(this, part);
|
|
||||||
|
|
||||||
if (Math.Abs(footDistance - float.MinValue) > 0.001f)
|
|
||||||
{
|
|
||||||
_activeLegs.Add(part, footDistance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CalculateSpeed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CalculateSpeed()
|
|
||||||
{
|
|
||||||
if (!Owner.TryGetComponent(out MovementSpeedModifierComponent? playerMover))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float speedSum = 0;
|
|
||||||
foreach (var part in _activeLegs.Keys)
|
|
||||||
{
|
|
||||||
if (!part.HasProperty<LegProperty>())
|
|
||||||
{
|
|
||||||
_activeLegs.Remove(part);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var (key, value) in _activeLegs)
|
|
||||||
{
|
|
||||||
if (key.TryGetProperty(out LegProperty? leg))
|
|
||||||
{
|
|
||||||
// Speed of a leg = base speed * (1+log1024(leg length))
|
|
||||||
speedSum += leg.Speed * (1 + (float) Math.Log(value, 1024.0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (speedSum <= 0.001f || _activeLegs.Count <= 0)
|
|
||||||
{
|
|
||||||
playerMover.BaseWalkSpeed = 0.8f;
|
|
||||||
playerMover.BaseSprintSpeed = 2.0f;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Extra legs stack diminishingly.
|
|
||||||
// Final speed = speed sum/(leg count-log4(leg count))
|
|
||||||
playerMover.BaseWalkSpeed =
|
|
||||||
speedSum / (_activeLegs.Count - (float) Math.Log(_activeLegs.Count, 4.0));
|
|
||||||
|
|
||||||
playerMover.BaseSprintSpeed = playerMover.BaseWalkSpeed * 1.75f;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,482 +200,6 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region BodyPart Functions
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Recursively searches for if <see cref="target"/> is connected to
|
|
||||||
/// the center. Not efficient (O(n^2)), but most bodies don't have a ton
|
|
||||||
/// of <see cref="IBodyPart"/>s.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="target">The body part to find the center for.</param>
|
|
||||||
/// <returns>True if it is connected to the center, false otherwise.</returns>
|
|
||||||
private bool ConnectedToCenterPart(IBodyPart target)
|
|
||||||
{
|
|
||||||
var searchedSlots = new List<string>();
|
|
||||||
|
|
||||||
return TryGetSlotName(target, out var result) &&
|
|
||||||
ConnectedToCenterPartRecursion(searchedSlots, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool ConnectedToCenterPartRecursion(ICollection<string> searchedSlots, string slotName)
|
|
||||||
{
|
|
||||||
if (!TryGetBodyPart(slotName, out var part))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (part == GetCenterBodyPart())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
searchedSlots.Add(slotName);
|
|
||||||
|
|
||||||
if (!TryGetBodyPartConnections(slotName, out List<string> connections))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var connection in connections)
|
|
||||||
{
|
|
||||||
if (!searchedSlots.Contains(connection) &&
|
|
||||||
ConnectedToCenterPartRecursion(searchedSlots, connection))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the central <see cref="IBodyPart"/>, if any, of this body based on
|
|
||||||
/// the <see cref="BodyTemplate"/>. For humans, this is the torso.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The <see cref="BodyPart"/> if one exists, null otherwise.</returns>
|
|
||||||
private IBodyPart? GetCenterBodyPart()
|
|
||||||
{
|
|
||||||
Parts.TryGetValue(Template.CenterSlot, out var center);
|
|
||||||
return center;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns whether the given slot name exists within the current
|
|
||||||
/// <see cref="BodyTemplate"/>.
|
|
||||||
/// </summary>
|
|
||||||
private bool SlotExists(string slotName)
|
|
||||||
{
|
|
||||||
return Template.SlotExists(slotName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the <see cref="IBodyPart"/> in the given <see cref="slotName"/> if
|
|
||||||
/// one exists.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot to search in.</param>
|
|
||||||
/// <param name="result">The body part in that slot, if any.</param>
|
|
||||||
/// <returns>True if found, false otherwise.</returns>
|
|
||||||
private bool TryGetBodyPart(string slotName, [NotNullWhen(true)] out IBodyPart? result)
|
|
||||||
{
|
|
||||||
return Parts.TryGetValue(slotName, out result!);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the slotName that the given <see cref="IBodyPart"/> resides in.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="part">The <see cref="IBodyPart"/> to find the slot for.</param>
|
|
||||||
/// <param name="result">The slot found, if any.</param>
|
|
||||||
/// <returns>True if a slot was found, false otherwise</returns>
|
|
||||||
private bool TryGetSlotName(IBodyPart part, [NotNullWhen(true)] out string result)
|
|
||||||
{
|
|
||||||
// We enforce that there is only one of each value in the dictionary,
|
|
||||||
// so we can iterate through the dictionary values to get the key from there.
|
|
||||||
var pair = Parts.FirstOrDefault(x => x.Value == part);
|
|
||||||
result = pair.Key;
|
|
||||||
|
|
||||||
return !pair.Equals(default);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the <see cref="BodyPartType"/> in the given
|
|
||||||
/// <see cref="slotName"/> if one exists.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot to search in.</param>
|
|
||||||
/// <param name="result">
|
|
||||||
/// The <see cref="BodyPartType"/> of that slot, if any.
|
|
||||||
/// </param>
|
|
||||||
/// <returns>True if found, false otherwise.</returns>
|
|
||||||
public bool TryGetSlotType(string slotName, out BodyPartType result)
|
|
||||||
{
|
|
||||||
return Template.Slots.TryGetValue(slotName, out result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Finds the names of all slots connected to the given
|
|
||||||
/// <see cref="slotName"/> for the template.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot to search in.</param>
|
|
||||||
/// <param name="connections">The connections found, if any.</param>
|
|
||||||
/// <returns>True if the connections are found, false otherwise.</returns>
|
|
||||||
private bool TryGetBodyPartConnections(string slotName, [NotNullWhen(true)] out List<string> connections)
|
|
||||||
{
|
|
||||||
return Template.Connections.TryGetValue(slotName, out connections!);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Grabs all occupied slots connected to the given slot,
|
|
||||||
/// regardless of whether the given <see cref="slotName"/> is occupied.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot name to find connections from.</param>
|
|
||||||
/// <param name="result">The connected body parts, if any.</param>
|
|
||||||
/// <returns>
|
|
||||||
/// True if successful, false if there was an error or no connected
|
|
||||||
/// <see cref="BodyPart"/>s were found.
|
|
||||||
/// </returns>
|
|
||||||
public bool TryGetBodyPartConnections(string slotName, [NotNullWhen(true)] out List<IBodyPart> result)
|
|
||||||
{
|
|
||||||
result = null!;
|
|
||||||
|
|
||||||
if (!Template.Connections.TryGetValue(slotName, out var connections))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var toReturn = new List<IBodyPart>();
|
|
||||||
foreach (var connection in connections)
|
|
||||||
{
|
|
||||||
if (TryGetBodyPart(connection, out var bodyPartResult))
|
|
||||||
{
|
|
||||||
toReturn.Add(bodyPartResult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toReturn.Count <= 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = toReturn;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Grabs all parts connected to the given <see cref="part"/>, regardless
|
|
||||||
/// of whether the given <see cref="part"/> is occupied.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// True if successful, false if there was an error or no connected
|
|
||||||
/// <see cref="IBodyPart"/>s were found.
|
|
||||||
/// </returns>
|
|
||||||
private bool TryGetBodyPartConnections(IBodyPart part, [NotNullWhen(true)] out List<IBodyPart> result)
|
|
||||||
{
|
|
||||||
result = null!;
|
|
||||||
|
|
||||||
return TryGetSlotName(part, out var slotName) &&
|
|
||||||
TryGetBodyPartConnections(slotName, out result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Grabs all <see cref="IBodyPart"/> of the given type in this body.
|
|
||||||
/// </summary>
|
|
||||||
public List<IBodyPart> GetBodyPartsOfType(BodyPartType type)
|
|
||||||
{
|
|
||||||
var toReturn = new List<IBodyPart>();
|
|
||||||
|
|
||||||
foreach (var part in Parts.Values)
|
|
||||||
{
|
|
||||||
if (part.PartType == type)
|
|
||||||
{
|
|
||||||
toReturn.Add(part);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return toReturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Installs the given <see cref="DroppedBodyPartComponent"/> into the
|
|
||||||
/// given slot, deleting the <see cref="IEntity"/> afterwards.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>True if successful, false otherwise.</returns>
|
|
||||||
public bool InstallDroppedBodyPart(DroppedBodyPartComponent part, string slotName)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(part);
|
|
||||||
|
|
||||||
if (!TryAddPart(slotName, part.ContainedBodyPart))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
part.Owner.Delete();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disconnects the given <see cref="IBodyPart"/> reference, potentially
|
|
||||||
/// dropping other <see cref="IBodyPart">BodyParts</see> if they were hanging
|
|
||||||
/// off of it.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IEntity"/> representing the dropped
|
|
||||||
/// <see cref="IBodyPart"/>, or null if none was dropped.
|
|
||||||
/// </returns>
|
|
||||||
public IEntity? DropPart(IBodyPart part)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(part);
|
|
||||||
|
|
||||||
if (!_parts.ContainsValue(part))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!RemoveBodyPart(part, out var slotName))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call disconnect on all limbs that were hanging off this limb.
|
|
||||||
if (TryGetBodyPartConnections(slotName, out List<string> connections))
|
|
||||||
{
|
|
||||||
// This loop is an unoptimized travesty. TODO: optimize to be less shit
|
|
||||||
foreach (var connectionName in connections)
|
|
||||||
{
|
|
||||||
if (TryGetBodyPart(connectionName, out var result) && !ConnectedToCenterPart(result))
|
|
||||||
{
|
|
||||||
DisconnectBodyPart(connectionName, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
part.SpawnDropped(out var dropped);
|
|
||||||
|
|
||||||
OnBodyChanged();
|
|
||||||
return dropped;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disconnects the given <see cref="IBodyPart"/> reference, potentially
|
|
||||||
/// dropping other <see cref="IBodyPart">BodyParts</see> if they were hanging
|
|
||||||
/// off of it.
|
|
||||||
/// </summary>
|
|
||||||
public void DisconnectBodyPart(IBodyPart part, bool dropEntity)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(part);
|
|
||||||
|
|
||||||
var slotName = _parts.FirstOrDefault(x => x.Value == part).Key;
|
|
||||||
if (string.IsNullOrEmpty(slotName)) return;
|
|
||||||
DisconnectBodyPart(slotName, dropEntity);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disconnects a body part in the given slot if one exists,
|
|
||||||
/// optionally dropping it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot to remove the body part from</param>
|
|
||||||
/// <param name="dropEntity">
|
|
||||||
/// Whether or not to drop the body part as an entity if it exists.
|
|
||||||
/// </param>
|
|
||||||
private void DisconnectBodyPart(string slotName, bool dropEntity)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(slotName);
|
|
||||||
|
|
||||||
if (!HasPart(slotName))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
RemoveBodyPart(slotName, dropEntity);
|
|
||||||
|
|
||||||
if (TryGetBodyPartConnections(slotName, out List<string> connections))
|
|
||||||
{
|
|
||||||
foreach (var connectionName in connections)
|
|
||||||
{
|
|
||||||
if (TryGetBodyPart(connectionName, out var result) && !ConnectedToCenterPart(result))
|
|
||||||
{
|
|
||||||
DisconnectBodyPart(connectionName, dropEntity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OnBodyChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryAddPart(string slot, IBodyPart part, bool force = false)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(part);
|
|
||||||
DebugTools.AssertNotNull(slot);
|
|
||||||
|
|
||||||
// Make sure the given slot exists
|
|
||||||
if (!force)
|
|
||||||
{
|
|
||||||
if (!SlotExists(slot))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// And that nothing is in it
|
|
||||||
if (!_parts.TryAdd(slot, part))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_parts[slot] = part;
|
|
||||||
}
|
|
||||||
|
|
||||||
part.Body = this;
|
|
||||||
|
|
||||||
var argsAdded = new BodyPartAddedEventArgs(part, slot);
|
|
||||||
|
|
||||||
foreach (var component in Owner.GetAllComponents<IBodyPartAdded>().ToArray())
|
|
||||||
{
|
|
||||||
component.BodyPartAdded(argsAdded);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Sort this duplicate out
|
|
||||||
OnBodyChanged();
|
|
||||||
|
|
||||||
if (!Template.Layers.TryGetValue(slot, out var partMap) ||
|
|
||||||
!_reflectionManager.TryParseEnumReference(partMap, out var partEnum))
|
|
||||||
{
|
|
||||||
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {partMap} for body part {part.Name}.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
part.RSIMap = partEnum;
|
|
||||||
|
|
||||||
var partMessage = new BodyPartAddedMessage(part.RSIPath, part.RSIState, partEnum);
|
|
||||||
|
|
||||||
SendNetworkMessage(partMessage);
|
|
||||||
|
|
||||||
foreach (var mechanism in part.Mechanisms)
|
|
||||||
{
|
|
||||||
if (!Template.MechanismLayers.TryGetValue(mechanism.Id, out var mechanismMap))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_reflectionManager.TryParseEnumReference(mechanismMap, out var mechanismEnum))
|
|
||||||
{
|
|
||||||
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {mechanismMap} for mechanism {mechanism.Id}.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mechanismMessage = new MechanismSpriteAddedMessage(mechanismEnum);
|
|
||||||
|
|
||||||
SendNetworkMessage(mechanismMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool HasPart(string slot)
|
|
||||||
{
|
|
||||||
return _parts.ContainsKey(slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the body part in slot <see cref="slotName"/> from this body,
|
|
||||||
/// if one exists.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="slotName">The slot to remove it from.</param>
|
|
||||||
/// <param name="drop">
|
|
||||||
/// Whether or not to drop the removed <see cref="IBodyPart"/>.
|
|
||||||
/// </param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private bool RemoveBodyPart(string slotName, bool drop)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(slotName);
|
|
||||||
|
|
||||||
if (!_parts.Remove(slotName, out var part))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
IEntity? dropped = null;
|
|
||||||
if (drop)
|
|
||||||
{
|
|
||||||
part.SpawnDropped(out dropped);
|
|
||||||
}
|
|
||||||
|
|
||||||
part.Body = null;
|
|
||||||
|
|
||||||
var args = new BodyPartRemovedEventArgs(part, slotName);
|
|
||||||
|
|
||||||
foreach (var component in Owner.GetAllComponents<IBodyPartRemoved>())
|
|
||||||
{
|
|
||||||
component.BodyPartRemoved(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (part.RSIMap != null)
|
|
||||||
{
|
|
||||||
var message = new BodyPartRemovedMessage(part.RSIMap, dropped?.Uid);
|
|
||||||
SendNetworkMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var mechanism in part.Mechanisms)
|
|
||||||
{
|
|
||||||
if (!Template.MechanismLayers.TryGetValue(mechanism.Id, out var mechanismMap))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_reflectionManager.TryParseEnumReference(mechanismMap, out var mechanismEnum))
|
|
||||||
{
|
|
||||||
Logger.Warning($"Template {Template.Name} has an invalid RSI map key {mechanismMap} for mechanism {mechanism.Id}.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mechanismMessage = new MechanismSpriteRemovedMessage(mechanismEnum);
|
|
||||||
|
|
||||||
SendNetworkMessage(mechanismMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CurrentDamageState == DamageState.Dead) return true;
|
|
||||||
|
|
||||||
// creadth: fall down if no legs
|
|
||||||
if (part.PartType == BodyPartType.Leg && Parts.Count(x => x.Value.PartType == BodyPartType.Leg) == 0)
|
|
||||||
{
|
|
||||||
EntitySystem.Get<StandingStateSystem>().Down(Owner);
|
|
||||||
}
|
|
||||||
|
|
||||||
// creadth: immediately kill entity if last vital part removed
|
|
||||||
if (part.IsVital && Parts.Count(x => x.Value.PartType == part.PartType) == 0)
|
|
||||||
{
|
|
||||||
CurrentDamageState = DamageState.Dead;
|
|
||||||
ForceHealthChangedEvent();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the body part from this body, if one exists.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="part">The part to remove from this body.</param>
|
|
||||||
/// <param name="slotName">The slot that the part was in, if any.</param>
|
|
||||||
/// <returns>True if <see cref="part"/> was removed, false otherwise.</returns>
|
|
||||||
private bool RemoveBodyPart(IBodyPart part, [NotNullWhen(true)] out string? slotName)
|
|
||||||
{
|
|
||||||
DebugTools.AssertNotNull(part);
|
|
||||||
|
|
||||||
var pair = _parts.FirstOrDefault(kvPair => kvPair.Value == part);
|
|
||||||
|
|
||||||
if (pair.Equals(default))
|
|
||||||
{
|
|
||||||
slotName = null;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
slotName = pair.Key;
|
|
||||||
|
|
||||||
return RemoveBodyPart(slotName, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region BodyNetwork Functions
|
#region BodyNetwork Functions
|
||||||
|
|
||||||
private bool EnsureNetwork(BodyNetwork network)
|
private bool EnsureNetwork(BodyNetwork network)
|
||||||
@@ -854,81 +277,6 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Recursion Functions
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the combined length of the distance to the nearest <see cref="BodyPart"/> with a
|
|
||||||
/// <see cref="FootProperty"/>. Returns <see cref="float.MinValue"/>
|
|
||||||
/// if there is no foot found. If you consider a <see cref="BodyManagerComponent"/> a node map, then it will look for
|
|
||||||
/// a foot node from the given node. It can
|
|
||||||
/// only search through BodyParts with <see cref="ExtensionProperty"/>.
|
|
||||||
/// </summary>
|
|
||||||
private static float DistanceToNearestFoot(BodyManagerComponent body, IBodyPart source)
|
|
||||||
{
|
|
||||||
if (source.HasProperty<FootProperty>() && source.TryGetProperty<ExtensionProperty>(out var property))
|
|
||||||
{
|
|
||||||
return property.ReachDistance;
|
|
||||||
}
|
|
||||||
|
|
||||||
return LookForFootRecursion(body, source, new List<BodyPart>());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Make this not static and not keep me up at night
|
|
||||||
private static float LookForFootRecursion(BodyManagerComponent body, IBodyPart current,
|
|
||||||
ICollection<BodyPart> searchedParts)
|
|
||||||
{
|
|
||||||
if (!current.TryGetProperty<ExtensionProperty>(out var extProperty))
|
|
||||||
{
|
|
||||||
return float.MinValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all connected parts if the current part has an extension property
|
|
||||||
if (!body.TryGetBodyPartConnections(current, out var connections))
|
|
||||||
{
|
|
||||||
return float.MinValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a connected BodyPart is a foot, return this BodyPart's length.
|
|
||||||
foreach (var connection in connections)
|
|
||||||
{
|
|
||||||
if (!searchedParts.Contains(connection) && connection.HasProperty<FootProperty>())
|
|
||||||
{
|
|
||||||
return extProperty.ReachDistance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, get the recursion values of all connected BodyParts and
|
|
||||||
// store them in a list.
|
|
||||||
var distances = new List<float>();
|
|
||||||
foreach (var connection in connections)
|
|
||||||
{
|
|
||||||
if (!searchedParts.Contains(connection))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = LookForFootRecursion(body, connection, searchedParts);
|
|
||||||
|
|
||||||
if (Math.Abs(result - float.MinValue) > 0.001f)
|
|
||||||
{
|
|
||||||
distances.Add(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If one or more of the searches found a foot, return the smallest one
|
|
||||||
// and add this ones length.
|
|
||||||
if (distances.Count > 0)
|
|
||||||
{
|
|
||||||
return distances.Min<float>() + extProperty.ReachDistance;
|
|
||||||
}
|
|
||||||
|
|
||||||
return float.MinValue;
|
|
||||||
|
|
||||||
// No extension property, no go.
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IBodyManagerHealthChangeParams
|
public interface IBodyManagerHealthChangeParams
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
{
|
{
|
||||||
if (!bodyManager.TryGetSlotType(slot, out var typeResult) ||
|
if (!bodyManager.TryGetSlotType(slot, out var typeResult) ||
|
||||||
typeResult != ContainedBodyPart?.PartType ||
|
typeResult != ContainedBodyPart?.PartType ||
|
||||||
!bodyManager.TryGetBodyPartConnections(slot, out var parts))
|
!bodyManager.TryGetPartConnections(slot, out var parts))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ namespace Content.Server.GameObjects.Components.Body
|
|||||||
var target = (string) targetObject!;
|
var target = (string) targetObject!;
|
||||||
string message;
|
string message;
|
||||||
|
|
||||||
if (_bodyManagerComponentCache.InstallDroppedBodyPart(this, target))
|
if (_bodyManagerComponentCache.TryAddPart(target, this))
|
||||||
{
|
{
|
||||||
message = Loc.GetString("You attach {0:theName}.", ContainedBodyPart);
|
message = Loc.GetString("You attach {0:theName}.", ContainedBodyPart);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,20 +6,14 @@ using Content.Shared.GameObjects.Components.Body;
|
|||||||
namespace Content.Server.GameObjects.Components.Body
|
namespace Content.Server.GameObjects.Components.Body
|
||||||
{
|
{
|
||||||
// TODO: Merge with ISharedBodyManagerComponent
|
// TODO: Merge with ISharedBodyManagerComponent
|
||||||
public interface IBodyManagerComponent : ISharedBodyManagerComponent
|
public interface IBodyManagerComponent : ISharedBodyManagerComponent, IBodyPartManager
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The <see cref="BodyTemplate"/> that this <see cref="BodyManagerComponent"/>
|
/// The <see cref="BodyTemplate"/> that this
|
||||||
/// is adhering to.
|
/// <see cref="BodyManagerComponent"/> is adhering to.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public BodyTemplate Template { get; }
|
public BodyTemplate Template { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The <see cref="BodyPreset"/> that this <see cref="BodyManagerComponent"/>
|
|
||||||
/// is adhering to.
|
|
||||||
/// </summary>
|
|
||||||
public BodyPreset Preset { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Installs the given <see cref="IBodyPart"/> into the given slot.
|
/// Installs the given <see cref="IBodyPart"/> into the given slot.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
154
Content.Server/GameObjects/Components/Body/IBodyPartManager.cs
Normal file
154
Content.Server/GameObjects/Components/Body/IBodyPartManager.cs
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Content.Server.Body;
|
||||||
|
using Content.Shared.GameObjects.Components.Body;
|
||||||
|
using Robust.Shared.Interfaces.GameObjects;
|
||||||
|
|
||||||
|
namespace Content.Server.GameObjects.Components.Body
|
||||||
|
{
|
||||||
|
public interface IBodyPartManager : IComponent
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="BodyPreset"/> that this
|
||||||
|
/// <see cref="BodyManagerComponent"/>
|
||||||
|
/// is adhering to.
|
||||||
|
/// </summary>
|
||||||
|
public BodyPreset Preset { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Installs the given <see cref="DroppedBodyPartComponent"/> into the
|
||||||
|
/// given slot, deleting the <see cref="IEntity"/> afterwards.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if successful, false otherwise.</returns>
|
||||||
|
bool TryAddPart(string slot, DroppedBodyPartComponent part, bool force = false);
|
||||||
|
|
||||||
|
bool TryAddPart(string slot, IBodyPart part, bool force = false);
|
||||||
|
|
||||||
|
bool HasPart(string slot);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the given <see cref="IBodyPart"/> reference, potentially
|
||||||
|
/// dropping other <see cref="IBodyPart">BodyParts</see> if they
|
||||||
|
/// were hanging off of it.
|
||||||
|
/// </summary>
|
||||||
|
void RemovePart(IBodyPart part, bool drop);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the body part in slot <see cref="slot"/> from this body,
|
||||||
|
/// if one exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The slot to remove it from.</param>
|
||||||
|
/// <param name="drop">
|
||||||
|
/// Whether or not to drop the removed <see cref="IBodyPart"/>.
|
||||||
|
/// </param>
|
||||||
|
/// <returns>True if the part was removed, false otherwise.</returns>
|
||||||
|
bool RemovePart(string slot, bool drop);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the body part from this body, if one exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="part">The part to remove from this body.</param>
|
||||||
|
/// <param name="slotName">The slot that the part was in, if any.</param>
|
||||||
|
/// <returns>True if <see cref="part"/> was removed, false otherwise.</returns>
|
||||||
|
bool RemovePart(IBodyPart part, [NotNullWhen(true)] out string? slotName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disconnects the given <see cref="IBodyPart"/> reference, potentially
|
||||||
|
/// dropping other <see cref="IBodyPart">BodyParts</see> if they were hanging
|
||||||
|
/// off of it.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The <see cref="IEntity"/> representing the dropped
|
||||||
|
/// <see cref="IBodyPart"/>, or null if none was dropped.
|
||||||
|
/// </returns>
|
||||||
|
IEntity? DropPart(IBodyPart part);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recursively searches for if <see cref="part"/> is connected to
|
||||||
|
/// the center.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="part">The body part to find the center for.</param>
|
||||||
|
/// <returns>True if it is connected to the center, false otherwise.</returns>
|
||||||
|
bool ConnectedToCenter(IBodyPart part);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the central <see cref="IBodyPart"/>, if any, of this body based on
|
||||||
|
/// the <see cref="BodyTemplate"/>. For humans, this is the torso.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The <see cref="BodyPart"/> if one exists, null otherwise.</returns>
|
||||||
|
IBodyPart? CenterPart();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns whether the given part slot name exists within the current
|
||||||
|
/// <see cref="BodyTemplate"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The slot to check for.</param>
|
||||||
|
/// <returns>True if the slot exists in this body, false otherwise.</returns>
|
||||||
|
bool HasSlot(string slot);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the <see cref="IBodyPart"/> in the given <see cref="slot"/> if
|
||||||
|
/// one exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The part slot to search in.</param>
|
||||||
|
/// <param name="result">The body part in that slot, if any.</param>
|
||||||
|
/// <returns>True if found, false otherwise.</returns>
|
||||||
|
bool TryGetPart(string slot, [NotNullWhen(true)] out IBodyPart? result);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the slotName that the given <see cref="IBodyPart"/> resides in.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="part">The <see cref="IBodyPart"/> to find the slot for.</param>
|
||||||
|
/// <param name="slot">The slot found, if any.</param>
|
||||||
|
/// <returns>True if a slot was found, false otherwise</returns>
|
||||||
|
bool TryGetSlot(IBodyPart part, [NotNullWhen(true)] out string? slot);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the <see cref="BodyPartType"/> in the given
|
||||||
|
/// <see cref="slot"/> if one exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The slot to search in.</param>
|
||||||
|
/// <param name="result">
|
||||||
|
/// The <see cref="BodyPartType"/> of that slot, if any.
|
||||||
|
/// </param>
|
||||||
|
/// <returns>True if found, false otherwise.</returns>
|
||||||
|
bool TryGetSlotType(string slot, out BodyPartType result);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the names of all slots connected to the given
|
||||||
|
/// <see cref="slot"/> for the template.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The slot to search in.</param>
|
||||||
|
/// <param name="connections">The connections found, if any.</param>
|
||||||
|
/// <returns>True if the connections are found, false otherwise.</returns>
|
||||||
|
bool TryGetSlotConnections(string slot, [NotNullWhen(true)] out List<string>? connections);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Grabs all occupied slots connected to the given slot,
|
||||||
|
/// regardless of whether the given <see cref="slot"/> is occupied.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slot">The slot name to find connections from.</param>
|
||||||
|
/// <param name="connections">The connected body parts, if any.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// True if successful, false if the slot couldn't be found on this body.
|
||||||
|
/// </returns>
|
||||||
|
bool TryGetPartConnections(string slot, [NotNullWhen(true)] out List<IBodyPart>? connections);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Grabs all parts connected to the given <see cref="part"/>, regardless
|
||||||
|
/// of whether the given <see cref="part"/> is occupied.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="part">The part to find connections from.</param>
|
||||||
|
/// <param name="connections">The connected body parts, if any.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// True if successful, false if the part couldn't be found on this body.
|
||||||
|
/// </returns>
|
||||||
|
bool TryGetPartConnections(IBodyPart part, [NotNullWhen(true)] out List<IBodyPart>? connections);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Grabs all <see cref="IBodyPart"/> of the given type in this body.
|
||||||
|
/// </summary>
|
||||||
|
List<IBodyPart> GetPartsOfType(BodyPartType type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -479,7 +479,7 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
|||||||
var headCount = 0;
|
var headCount = 0;
|
||||||
if (victim.TryGetComponent<BodyManagerComponent>(out var bodyManagerComponent))
|
if (victim.TryGetComponent<BodyManagerComponent>(out var bodyManagerComponent))
|
||||||
{
|
{
|
||||||
var heads = bodyManagerComponent.GetBodyPartsOfType(BodyPartType.Head);
|
var heads = bodyManagerComponent.GetPartsOfType(BodyPartType.Head);
|
||||||
foreach (var head in heads)
|
foreach (var head in heads)
|
||||||
{
|
{
|
||||||
var droppedHead = bodyManagerComponent.DropPart(head);
|
var droppedHead = bodyManagerComponent.DropPart(head);
|
||||||
|
|||||||
@@ -101,8 +101,8 @@ namespace Content.Server.GameObjects.Components.Movement
|
|||||||
|
|
||||||
var bodyManager = user.GetComponent<BodyManagerComponent>();
|
var bodyManager = user.GetComponent<BodyManagerComponent>();
|
||||||
|
|
||||||
if (bodyManager.GetBodyPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Leg).Count == 0 ||
|
if (bodyManager.GetPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Leg).Count == 0 ||
|
||||||
bodyManager.GetBodyPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Foot).Count == 0)
|
bodyManager.GetPartsOfType(Shared.GameObjects.Components.Body.BodyPartType.Foot).Count == 0)
|
||||||
{
|
{
|
||||||
reason = Loc.GetString("You are unable to climb!");
|
reason = Loc.GetString("You are unable to climb!");
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user