Refactoring body system to use containers and general body cleanup (#20202)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -118,7 +118,7 @@ namespace Content.Server.Body.Commands
|
||||
}
|
||||
}
|
||||
|
||||
if (!_entManager.TryGetComponent(entity, out BodyComponent? body) || body.Root == null)
|
||||
if (!_entManager.TryGetComponent(entity, out BodyComponent? body) || body.RootContainer.ContainedEntity == null)
|
||||
{
|
||||
var text = $"You have no body{(_random.Prob(0.2f) ? " and you must scream." : ".")}";
|
||||
|
||||
@@ -140,7 +140,7 @@ namespace Content.Server.Body.Commands
|
||||
|
||||
var slotId = part.GetHashCode().ToString();
|
||||
|
||||
if (!bodySystem.TryCreatePartSlotAndAttach(attachAt.Id, slotId, hand, attachAt.Component, part))
|
||||
if (!bodySystem.TryCreatePartSlotAndAttach(attachAt.Id, slotId, hand, BodyPartType.Hand,attachAt.Component, part))
|
||||
{
|
||||
shell.WriteError($"Couldn't create a slot with id {slotId} on entity {_entManager.ToPrettyString(entity)}");
|
||||
return;
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace Content.Server.Body.Commands
|
||||
}
|
||||
|
||||
var bodySystem = _entManager.System<BodySystem>();
|
||||
if (bodySystem.BodyHasChild(bodyId, partUid, body, part))
|
||||
if (bodySystem.BodyHasChild(bodyId, partUid.Value, body, part))
|
||||
{
|
||||
shell.WriteLine($"Body part {_entManager.GetComponent<MetaDataComponent>(partUid.Value).EntityName} with uid {partUid} is already attached to entity {_entManager.GetComponent<MetaDataComponent>(bodyId).EntityName} with uid {bodyId}");
|
||||
return;
|
||||
@@ -103,16 +103,14 @@ namespace Content.Server.Body.Commands
|
||||
var slotId = $"AttachBodyPartVerb-{partUid}";
|
||||
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (bodySystem.TryCreateBodyRootSlot(bodyId, slotId, out var rootSlot, body))
|
||||
if (body.RootContainer.ContainedEntity != null)
|
||||
{
|
||||
bodySystem.DropPart(partUid, part);
|
||||
bodySystem.AttachPart(partUid, rootSlot, part);
|
||||
bodySystem.AttachPartToRoot(bodyId,partUid.Value, body ,part);
|
||||
}
|
||||
else
|
||||
{
|
||||
var attachAt = bodySystem.GetBodyChildren(bodyId, body).First();
|
||||
|
||||
if (!bodySystem.TryCreatePartSlotAndAttach(attachAt.Id, slotId, partUid, attachAt.Component, part))
|
||||
var (rootPartId,rootPart) = bodySystem.GetRootPartOrNull(bodyId, body)!.Value;
|
||||
if (!bodySystem.TryCreatePartSlotAndAttach(rootPartId, slotId, partUid.Value, part.PartType, rootPart, part))
|
||||
{
|
||||
shell.WriteError($"Could not create slot {slotId} on entity {_entManager.ToPrettyString(bodyId)}");
|
||||
return;
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace Content.Server.Body.Commands
|
||||
{
|
||||
if (fac.GetComponentName(organ.Component.GetType()).ToLowerInvariant() == mechanismName)
|
||||
{
|
||||
bodySystem.DeleteOrgan(organ.Id, organ.Component);
|
||||
entityManager.QueueDeleteEntity(organ.Id);
|
||||
shell.WriteLine($"Mechanism with name {mechanismName} has been destroyed.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,11 @@ using Robust.Shared.Random;
|
||||
namespace Content.Server.Body.Commands
|
||||
{
|
||||
[AdminCommand(AdminFlags.Fun)]
|
||||
sealed class RemoveHandCommand : IConsoleCommand
|
||||
public sealed class RemoveHandCommand : IConsoleCommand
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public string Command => "removehand";
|
||||
public string Description => "Removes a hand from your entity.";
|
||||
public string Help => $"Usage: {Command}";
|
||||
@@ -32,18 +35,16 @@ namespace Content.Server.Body.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
if (!entityManager.TryGetComponent(player.AttachedEntity, out BodyComponent? body))
|
||||
if (!_entManager.TryGetComponent(player.AttachedEntity, out BodyComponent? body))
|
||||
{
|
||||
var random = IoCManager.Resolve<IRobustRandom>();
|
||||
var text = $"You have no body{(random.Prob(0.2f) ? " and you must scream." : ".")}";
|
||||
var text = $"You have no body{(_random.Prob(0.2f) ? " and you must scream." : ".")}";
|
||||
|
||||
shell.WriteLine(text);
|
||||
return;
|
||||
}
|
||||
|
||||
var bodySystem = entityManager.System<BodySystem>();
|
||||
var hand = bodySystem.GetBodyChildrenOfType(player.AttachedEntity, BodyPartType.Hand, body).FirstOrDefault();
|
||||
var bodySystem = _entManager.System<BodySystem>();
|
||||
var hand = bodySystem.GetBodyChildrenOfType(player.AttachedEntity.Value, BodyPartType.Hand, body).FirstOrDefault();
|
||||
|
||||
if (hand == default)
|
||||
{
|
||||
@@ -51,7 +52,7 @@ namespace Content.Server.Body.Commands
|
||||
}
|
||||
else
|
||||
{
|
||||
bodySystem.DropPart(hand.Id, hand.Component);
|
||||
_entManager.System<SharedTransformSystem>().AttachToGridOrMap(hand.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
@@ -32,80 +33,16 @@ public sealed class BodySystem : SharedBodySystem
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
||||
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<BodyPartComponent, ComponentStartup>(OnPartStartup);
|
||||
SubscribeLocalEvent<BodyComponent, ComponentStartup>(OnBodyStartup);
|
||||
SubscribeLocalEvent<BodyComponent, MoveInputEvent>(OnRelayMoveInput);
|
||||
SubscribeLocalEvent<BodyComponent, ApplyMetabolicMultiplierEvent>(OnApplyMetabolicMultiplier);
|
||||
SubscribeLocalEvent<BodyComponent, BeingMicrowavedEvent>(OnBeingMicrowaved);
|
||||
}
|
||||
|
||||
private void OnPartStartup(EntityUid uid, BodyPartComponent component, ComponentStartup args)
|
||||
{
|
||||
// This inter-entity relationship makes be deeply uncomfortable because its probably going to re-encounter
|
||||
// all of the networking & startup ordering issues that containers and joints have.
|
||||
// TODO just use containers. Please.
|
||||
|
||||
foreach (var slot in component.Children.Values)
|
||||
{
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
continue;
|
||||
|
||||
if (TryComp(slot.Child, out BodyPartComponent? child))
|
||||
{
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.Error($"Body part encountered missing limbs: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
}
|
||||
|
||||
foreach (var slot in component.Organs.Values)
|
||||
{
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
continue;
|
||||
|
||||
if (TryComp(slot.Child, out OrganComponent? child))
|
||||
{
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
continue;
|
||||
}
|
||||
|
||||
Log.Error($"Body part encountered missing organ: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnBodyStartup(EntityUid uid, BodyComponent component, ComponentStartup args)
|
||||
{
|
||||
if (component.Root is not { } slot)
|
||||
return;
|
||||
|
||||
DebugTools.Assert(slot.Parent == uid);
|
||||
if (slot.Child == null)
|
||||
return;
|
||||
|
||||
if (!TryComp(slot.Child, out BodyPartComponent? child))
|
||||
{
|
||||
Log.Error($"Body part encountered missing limbs: {ToPrettyString(uid)}. Slot: {slot.Id}");
|
||||
slot.Child = null;
|
||||
return;
|
||||
}
|
||||
|
||||
child.ParentSlot = slot;
|
||||
Dirty(slot.Child.Value, child);
|
||||
}
|
||||
|
||||
private void OnRelayMoveInput(EntityUid uid, BodyComponent component, ref MoveInputEvent args)
|
||||
{
|
||||
if (_mobState.IsDead(uid) && _mindSystem.TryGetMind(uid, out var mindId, out var mind))
|
||||
@@ -130,98 +67,95 @@ public sealed class BodySystem : SharedBodySystem
|
||||
return;
|
||||
|
||||
// Don't microwave animals, kids
|
||||
_transform.AttachToGridOrMap(uid);
|
||||
SharedTransform.AttachToGridOrMap(uid);
|
||||
_appearance.SetData(args.Microwave, MicrowaveVisualState.Bloody, true);
|
||||
GibBody(uid, false, component);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
public override bool AttachPart(
|
||||
EntityUid? partId,
|
||||
BodyPartSlot slot,
|
||||
[NotNullWhen(true)] BodyPartComponent? part = null)
|
||||
protected override void AddPart(
|
||||
EntityUid bodyUid,
|
||||
EntityUid partUid,
|
||||
string slotId,
|
||||
BodyPartComponent component,
|
||||
BodyComponent? bodyComp = null)
|
||||
{
|
||||
if (!base.AttachPart(partId, slot, part))
|
||||
return false;
|
||||
// TODO: Predict this probably.
|
||||
base.AddPart(bodyUid, partUid, slotId, component, bodyComp);
|
||||
|
||||
if (part.Body is { } body &&
|
||||
TryComp<HumanoidAppearanceComponent>(body, out var humanoid))
|
||||
if (TryComp<HumanoidAppearanceComponent>(bodyUid, out var humanoid))
|
||||
{
|
||||
var layer = part.ToHumanoidLayers();
|
||||
var layer = component.ToHumanoidLayers();
|
||||
if (layer != null)
|
||||
{
|
||||
var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
|
||||
_humanoidSystem.SetLayersVisibility(body, layers, true, true, humanoid);
|
||||
_humanoidSystem.SetLayersVisibility(bodyUid, layers, true, true, humanoid);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool DropPart(EntityUid? partId, BodyPartComponent? part = null)
|
||||
protected override void RemovePart(
|
||||
EntityUid bodyUid,
|
||||
EntityUid partUid,
|
||||
string slotId,
|
||||
BodyPartComponent component,
|
||||
BodyComponent? bodyComp = null)
|
||||
{
|
||||
if (partId == null || !Resolve(partId.Value, ref part))
|
||||
return false;
|
||||
base.RemovePart(bodyUid, partUid, slotId, component, bodyComp);
|
||||
|
||||
if (!base.DropPart(partId, part))
|
||||
return false;
|
||||
if (!TryComp<HumanoidAppearanceComponent>(bodyUid, out var humanoid))
|
||||
return;
|
||||
|
||||
var oldBody = part.Body;
|
||||
if (oldBody == null || !TryComp<HumanoidAppearanceComponent>(oldBody, out var humanoid))
|
||||
return true;
|
||||
var layer = component.ToHumanoidLayers();
|
||||
|
||||
var layer = part.ToHumanoidLayers();
|
||||
if (layer == null)
|
||||
return true;
|
||||
return;
|
||||
|
||||
var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
|
||||
_humanoidSystem.SetLayersVisibility(oldBody.Value, layers, false, true, humanoid);
|
||||
return true;
|
||||
_humanoidSystem.SetLayersVisibility(bodyUid, layers, false, true, humanoid);
|
||||
}
|
||||
|
||||
public override HashSet<EntityUid> GibBody(EntityUid? bodyId, bool gibOrgans = false, BodyComponent? body = null, bool deleteItems = false)
|
||||
public override HashSet<EntityUid> GibBody(EntityUid bodyId, bool gibOrgans = false, BodyComponent? body = null, bool deleteItems = false)
|
||||
{
|
||||
if (bodyId == null || !Resolve(bodyId.Value, ref body, false))
|
||||
if (!Resolve(bodyId, ref body, false))
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
if (LifeStage(bodyId.Value) >= EntityLifeStage.Terminating || EntityManager.IsQueuedForDeletion(bodyId.Value))
|
||||
if (LifeStage(bodyId) >= EntityLifeStage.Terminating || EntityManager.IsQueuedForDeletion(bodyId))
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
var xform = Transform(bodyId.Value);
|
||||
var xform = Transform(bodyId);
|
||||
if (xform.MapUid == null)
|
||||
return new HashSet<EntityUid>();
|
||||
|
||||
var gibs = base.GibBody(bodyId, gibOrgans, body, deleteItems);
|
||||
|
||||
var coordinates = xform.Coordinates;
|
||||
var filter = Filter.Pvs(bodyId.Value, entityManager: EntityManager);
|
||||
var filter = Filter.Pvs(bodyId, entityManager: EntityManager);
|
||||
var audio = AudioParams.Default.WithVariation(0.025f);
|
||||
|
||||
_audio.Play(body.GibSound, filter, coordinates, true, audio);
|
||||
|
||||
if (TryComp(bodyId, out ContainerManagerComponent? container))
|
||||
var containers = GetBodyContainers(bodyId, body: body).ToList();
|
||||
|
||||
foreach (var container in containers)
|
||||
{
|
||||
foreach (var cont in container.GetAllContainers().ToArray())
|
||||
foreach (var entity in container.ContainedEntities)
|
||||
{
|
||||
foreach (var ent in cont.ContainedEntities.ToArray())
|
||||
if (deleteItems)
|
||||
{
|
||||
if (deleteItems)
|
||||
{
|
||||
QueueDel(ent);
|
||||
}
|
||||
else
|
||||
{
|
||||
cont.Remove(ent, EntityManager, force: true);
|
||||
Transform(ent).Coordinates = coordinates;
|
||||
ent.RandomOffset(0.25f);
|
||||
}
|
||||
QueueDel(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
container.Remove(entity, EntityManager, force: true);
|
||||
SharedTransform.SetCoordinates(entity,coordinates);
|
||||
entity.RandomOffset(0.25f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RaiseLocalEvent(bodyId.Value, new BeingGibbedEvent(gibs));
|
||||
QueueDel(bodyId.Value);
|
||||
RaiseLocalEvent(bodyId, new BeingGibbedEvent(gibs));
|
||||
QueueDel(bodyId);
|
||||
|
||||
return gibs;
|
||||
}
|
||||
|
||||
@@ -30,17 +30,18 @@ namespace Content.Server.Body.Systems
|
||||
private void OnRemovedFromBody(EntityUid uid, BrainComponent component, RemovedFromBodyEvent args)
|
||||
{
|
||||
// This one needs to be special, okay?
|
||||
if (!EntityManager.TryGetComponent(uid, out OrganComponent? organ) ||
|
||||
organ.ParentSlot is not {Parent: var parent})
|
||||
if (!EntityManager.TryGetComponent(uid, out OrganComponent? organ))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HandleMind(parent, args.Old);
|
||||
HandleMind(uid, args.Old);
|
||||
}
|
||||
|
||||
private void HandleMind(EntityUid newEntity, EntityUid oldEntity)
|
||||
{
|
||||
EnsureComp<MindContainerComponent>(newEntity);
|
||||
var oldMind = EnsureComp<MindContainerComponent>(oldEntity);
|
||||
EnsureComp<MindContainerComponent>(oldEntity);
|
||||
|
||||
var ghostOnMove = EnsureComp<GhostOnMoveComponent>(newEntity);
|
||||
if (HasComp<BodyComponent>(newEntity))
|
||||
|
||||
Reference in New Issue
Block a user