Body code cleanup (#24946)

* Fix test

* Kill float accumulators

* Use entity proxy methods

* DataField auto name generation where possible

* Kill comp properties

* Clean up server comps

* Make events record structs

* Clean up shared body code

* Clean up server body code

* Rename organ events to be same names as in med refactor
This commit is contained in:
0x6273
2024-03-28 01:48:37 +01:00
committed by GitHub
parent 527c2c42ed
commit 37b8d78dac
32 changed files with 916 additions and 820 deletions

View File

@@ -202,7 +202,7 @@ namespace Content.Shared.ActionBlocker
public bool CanShiver(EntityUid uid)
{
var ev = new ShiverAttemptEvent(uid);
RaiseLocalEvent(uid, ev);
RaiseLocalEvent(uid, ref ev);
return !ev.Cancelled;
}
@@ -210,7 +210,7 @@ namespace Content.Shared.ActionBlocker
public bool CanSweat(EntityUid uid)
{
var ev = new SweatAttemptEvent(uid);
RaiseLocalEvent(uid, ev);
RaiseLocalEvent(uid, ref ev);
return !ev.Cancelled;
}

View File

@@ -1,61 +1,28 @@
namespace Content.Shared.Body.Events
{
// All of these events are raised on a mechanism entity when added/removed to a body in different
// ways.
namespace Content.Shared.Body.Events;
/// <summary>
/// Raised on a mechanism when it is added to a body part.
/// </summary>
public sealed class AddedToPartEvent : EntityEventArgs
{
public EntityUid Part;
// All of these events are raised on a mechanism entity when added/removed to a body in different
// ways.
public AddedToPartEvent(EntityUid part)
{
Part = part;
}
}
/// <summary>
/// Raised on a mechanism when it is added to a body part.
/// </summary>
[ByRefEvent]
public readonly record struct OrganAddedEvent(EntityUid Part);
/// <summary>
/// Raised on a mechanism when it is added to a body part within a body.
/// </summary>
public sealed class AddedToPartInBodyEvent : EntityEventArgs
{
public EntityUid Body;
public EntityUid Part;
/// <summary>
/// Raised on a mechanism when it is added to a body part within a body.
/// </summary>
[ByRefEvent]
public readonly record struct OrganAddedToBodyEvent(EntityUid Body, EntityUid Part);
public AddedToPartInBodyEvent(EntityUid body, EntityUid part)
{
Body = body;
Part = part;
}
}
/// <summary>
/// Raised on a mechanism when it is removed from a body part.
/// </summary>
[ByRefEvent]
public readonly record struct OrganRemovedEvent(EntityUid OldPart);
/// <summary>
/// Raised on a mechanism when it is removed from a body part.
/// </summary>
public sealed class RemovedFromPartEvent : EntityEventArgs
{
public EntityUid OldPart;
public RemovedFromPartEvent(EntityUid oldPart)
{
OldPart = oldPart;
}
}
/// <summary>
/// Raised on a mechanism when it is removed from a body part within a body.
/// </summary>
public sealed class RemovedFromPartInBodyEvent : EntityEventArgs
{
public EntityUid OldBody;
public EntityUid OldPart;
public RemovedFromPartInBodyEvent(EntityUid oldBody, EntityUid oldPart)
{
OldBody = oldBody;
OldPart = oldPart;
}
}
}
/// <summary>
/// Raised on a mechanism when it is removed from a body part within a body.
/// </summary>
[ByRefEvent]
public readonly record struct OrganRemovedFromBodyEvent(EntityUid OldBody, EntityUid OldPart);

View File

@@ -1,12 +1,8 @@
namespace Content.Shared.Body.Events
{
public sealed class ShiverAttemptEvent : CancellableEntityEventArgs
{
public ShiverAttemptEvent(EntityUid uid)
{
Uid = uid;
}
namespace Content.Shared.Body.Events;
public EntityUid Uid { get; }
}
[ByRefEvent]
public record struct ShiverAttemptEvent(EntityUid Uid)
{
public readonly EntityUid Uid = Uid;
public bool Cancelled = false;
}

View File

@@ -1,12 +1,8 @@
namespace Content.Shared.Body.Events
{
public sealed class SweatAttemptEvent : CancellableEntityEventArgs
{
public SweatAttemptEvent(EntityUid uid)
{
Uid = uid;
}
namespace Content.Shared.Body.Events;
public EntityUid Uid { get; }
}
[ByRefEvent]
public record struct SweatAttemptEvent(EntityUid Uid)
{
public readonly EntityUid Uid = Uid;
public bool Cancelled = false;
}

View File

@@ -1,4 +1,4 @@
using Content.Shared.Body.Systems;
using Content.Shared.Body.Systems;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
@@ -11,6 +11,6 @@ public sealed partial class OrganComponent : Component
/// <summary>
/// Relevant body this organ is attached to.
/// </summary>
[DataField("body"), AutoNetworkedField]
[DataField, AutoNetworkedField]
public EntityUid? Body;
}

View File

@@ -1,7 +1,7 @@
namespace Content.Shared.Body.Part;
namespace Content.Shared.Body.Part;
[ByRefEvent]
public readonly record struct BodyPartAddedEvent(string Slot, BodyPartComponent Part);
public readonly record struct BodyPartAddedEvent(string Slot, Entity<BodyPartComponent> Part);
[ByRefEvent]
public readonly record struct BodyPartRemovedEvent(string Slot, BodyPartComponent Part);
public readonly record struct BodyPartRemovedEvent(string Slot, Entity<BodyPartComponent> Part);

View File

@@ -9,7 +9,6 @@ using Content.Shared.Gibbing.Components;
using Content.Shared.Gibbing.Events;
using Content.Shared.Gibbing.Systems;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
@@ -30,8 +29,10 @@ public partial class SharedBodySystem
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly GibbingSystem _gibbingSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
private const float GibletLaunchImpulse = 8;
private const float GibletLaunchImpulseVariance = 3;
private void InitializeBody()
{
// Body here to handle root body parts.
@@ -43,7 +44,7 @@ public partial class SharedBodySystem
SubscribeLocalEvent<BodyComponent, CanDragEvent>(OnBodyCanDrag);
}
private void OnBodyInserted(EntityUid uid, BodyComponent component, EntInsertedIntoContainerMessage args)
private void OnBodyInserted(Entity<BodyComponent> ent, ref EntInsertedIntoContainerMessage args)
{
// Root body part?
var slotId = args.Container.ID;
@@ -51,21 +52,21 @@ public partial class SharedBodySystem
if (slotId != BodyRootContainerId)
return;
var entity = args.Entity;
var insertedUid = args.Entity;
if (TryComp(entity, out BodyPartComponent? childPart))
if (TryComp(insertedUid, out BodyPartComponent? part))
{
AddPart(uid, entity, slotId, childPart);
RecursiveBodyUpdate(entity, uid, childPart);
AddPart((ent, ent), (insertedUid, part), slotId);
RecursiveBodyUpdate((insertedUid, part), ent);
}
if (TryComp(entity, out OrganComponent? organ))
if (TryComp(insertedUid, out OrganComponent? organ))
{
AddOrgan(entity, uid, uid, organ);
AddOrgan((insertedUid, organ), ent, ent);
}
}
private void OnBodyRemoved(EntityUid uid, BodyComponent component, EntRemovedFromContainerMessage args)
private void OnBodyRemoved(Entity<BodyComponent> ent, ref EntRemovedFromContainerMessage args)
{
// Root body part?
var slotId = args.Container.ID;
@@ -73,55 +74,55 @@ public partial class SharedBodySystem
if (slotId != BodyRootContainerId)
return;
var entity = args.Entity;
DebugTools.Assert(!TryComp(entity, out BodyPartComponent? b) || b.Body == uid);
DebugTools.Assert(!TryComp(entity, out OrganComponent? o) || o.Body == uid);
var removedUid = args.Entity;
DebugTools.Assert(!TryComp(removedUid, out BodyPartComponent? b) || b.Body == ent);
DebugTools.Assert(!TryComp(removedUid, out OrganComponent? o) || o.Body == ent);
if (TryComp(entity, out BodyPartComponent? childPart))
if (TryComp(removedUid, out BodyPartComponent? part))
{
RemovePart(uid, entity, slotId, childPart);
RecursiveBodyUpdate(entity, null, childPart);
RemovePart((ent, ent), (removedUid, part), slotId);
RecursiveBodyUpdate((removedUid, part), null);
}
if (TryComp(entity, out OrganComponent? organ))
RemoveOrgan(entity, uid, organ);
if (TryComp(removedUid, out OrganComponent? organ))
RemoveOrgan((removedUid, organ), ent);
}
private void OnBodyInit(EntityUid bodyId, BodyComponent body, ComponentInit args)
private void OnBodyInit(Entity<BodyComponent> ent, ref ComponentInit args)
{
// Setup the initial container.
body.RootContainer = Containers.EnsureContainer<ContainerSlot>(bodyId, BodyRootContainerId);
ent.Comp.RootContainer = Containers.EnsureContainer<ContainerSlot>(ent, BodyRootContainerId);
}
private void OnBodyMapInit(EntityUid bodyId, BodyComponent body, MapInitEvent args)
private void OnBodyMapInit(Entity<BodyComponent> ent, ref MapInitEvent args)
{
if (body.Prototype == null)
if (ent.Comp.Prototype is null)
return;
// One-time setup
// Obviously can't run in Init to avoid double-spawns on save / load.
var prototype = Prototypes.Index(body.Prototype.Value);
MapInitBody(bodyId, prototype);
var prototype = Prototypes.Index(ent.Comp.Prototype.Value);
MapInitBody(ent, prototype);
}
private void MapInitBody(EntityUid bodyEntity, BodyPrototype prototype)
{
var protoRoot = prototype.Slots[prototype.Root];
if (protoRoot.Part == null)
if (protoRoot.Part is null)
return;
// This should already handle adding the entity to the root.
var rootPartEntity = SpawnInContainerOrDrop(protoRoot.Part, bodyEntity, BodyRootContainerId);
var rootPart = Comp<BodyPartComponent>(rootPartEntity);
var rootPartUid = SpawnInContainerOrDrop(protoRoot.Part, bodyEntity, BodyRootContainerId);
var rootPart = Comp<BodyPartComponent>(rootPartUid);
rootPart.Body = bodyEntity;
Dirty(rootPartEntity, rootPart);
Dirty(rootPartUid, rootPart);
// Setup the rest of the body entities.
SetupOrgans(rootPartEntity, rootPart, protoRoot.Organs);
MapInitParts(rootPartEntity, prototype);
SetupOrgans((rootPartUid, rootPart), protoRoot.Organs);
MapInitParts(rootPartUid, prototype);
}
private void OnBodyCanDrag(EntityUid uid, BodyComponent component, ref CanDragEvent args)
private void OnBodyCanDrag(Entity<BodyComponent> ent, ref CanDragEvent args)
{
args.Handled = true;
}
@@ -169,7 +170,7 @@ public partial class SharedBodySystem
var partSlot = CreatePartSlot(parentEntity, connection, childPartComponent.PartType, parentPartComponent);
var cont = Containers.GetContainer(parentEntity, GetPartSlotContainerId(connection));
if (partSlot == null || !Containers.Insert(childPart, cont))
if (partSlot is null || !Containers.Insert(childPart, cont))
{
Log.Error($"Could not create slot for connection {connection} in body {prototype.ID}");
QueueDel(childPart);
@@ -177,7 +178,7 @@ public partial class SharedBodySystem
}
// Add organs
SetupOrgans(childPart, childPartComponent, connectionSlot.Organs);
SetupOrgans((childPart, childPartComponent), connectionSlot.Organs);
// Enqueue it so we can also get its neighbors.
frontier.Enqueue(connection);
@@ -185,16 +186,16 @@ public partial class SharedBodySystem
}
}
private void SetupOrgans(EntityUid partId, BodyPartComponent partComponent, Dictionary<string, string> organs)
private void SetupOrgans(Entity<BodyPartComponent> ent, Dictionary<string, string> organs)
{
foreach (var (organSlotId, organProto) in organs)
{
var slot = CreateOrganSlot(organSlotId, partId, partComponent);
SpawnInContainerOrDrop(organProto, partId, GetOrganContainerId(organSlotId));
var slot = CreateOrganSlot((ent, ent), organSlotId);
SpawnInContainerOrDrop(organProto, ent, GetOrganContainerId(organSlotId));
if (slot == null)
if (slot is null)
{
Log.Error($"Could not create organ for slot {organSlotId} in {ToPrettyString(partId)}");
Log.Error($"Could not create organ for slot {organSlotId} in {ToPrettyString(ent)}");
}
}
}
@@ -202,12 +203,14 @@ public partial class SharedBodySystem
/// <summary>
/// Gets all body containers on this entity including the root one.
/// </summary>
public IEnumerable<BaseContainer> GetBodyContainers(EntityUid id, BodyComponent? body = null,
public IEnumerable<BaseContainer> GetBodyContainers(
EntityUid id,
BodyComponent? body = null,
BodyPartComponent? rootPart = null)
{
if (!Resolve(id, ref body, false) ||
body.RootContainer.ContainedEntity == null ||
!Resolve(body.RootContainer.ContainedEntity.Value, ref rootPart))
if (!Resolve(id, ref body, logMissing: false)
|| body.RootContainer.ContainedEntity is null
|| !Resolve(body.RootContainer.ContainedEntity.Value, ref rootPart))
{
yield break;
}
@@ -223,13 +226,15 @@ public partial class SharedBodySystem
/// <summary>
/// Gets all child body parts of this entity, including the root entity.
/// </summary>
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyChildren(EntityUid? id, BodyComponent? body = null,
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyChildren(
EntityUid? id,
BodyComponent? body = null,
BodyPartComponent? rootPart = null)
{
if (id == null ||
!Resolve(id.Value, ref body, false) ||
body.RootContainer.ContainedEntity == null ||
!Resolve(body.RootContainer.ContainedEntity.Value, ref rootPart))
if (id is null
|| !Resolve(id.Value, ref body, logMissing: false)
|| body.RootContainer.ContainedEntity is null
|| !Resolve(body.RootContainer.ContainedEntity.Value, ref rootPart))
{
yield break;
}
@@ -240,9 +245,11 @@ public partial class SharedBodySystem
}
}
public IEnumerable<(EntityUid Id, OrganComponent Component)> GetBodyOrgans(EntityUid? bodyId, BodyComponent? body = null)
public IEnumerable<(EntityUid Id, OrganComponent Component)> GetBodyOrgans(
EntityUid? bodyId,
BodyComponent? body = null)
{
if (bodyId == null || !Resolve(bodyId.Value, ref body, false))
if (bodyId is null || !Resolve(bodyId.Value, ref body, logMissing: false))
yield break;
foreach (var part in GetBodyChildren(bodyId, body))
@@ -260,10 +267,15 @@ public partial class SharedBodySystem
/// <param name="bodyId"></param>
/// <param name="body"></param>
/// <returns></returns>
public IEnumerable<BodyPartSlot> GetBodyAllSlots(EntityUid bodyId, BodyComponent? body = null)
public IEnumerable<BodyPartSlot> GetBodyAllSlots(
EntityUid bodyId,
BodyComponent? body = null)
{
if (!Resolve(bodyId, ref body, false) || body.RootContainer.ContainedEntity == null)
if (!Resolve(bodyId, ref body, logMissing: false)
|| body.RootContainer.ContainedEntity is null)
{
yield break;
}
foreach (var slot in GetAllBodyPartSlots(body.RootContainer.ContainedEntity.Value))
{
@@ -279,12 +291,11 @@ public partial class SharedBodySystem
Vector2? splatDirection = null,
float splatModifier = 1,
Angle splatCone = default,
SoundSpecifier? gibSoundOverride = null
)
SoundSpecifier? gibSoundOverride = null)
{
var gibs = new HashSet<EntityUid>();
if (!Resolve(bodyId, ref body, false))
if (!Resolve(bodyId, ref body, logMissing: false))
return gibs;
var root = GetRootPartOrNull(bodyId, body);
@@ -311,7 +322,7 @@ public partial class SharedBodySystem
launchImpulseVariance:GibletLaunchImpulseVariance, launchCone: splatCone);
}
}
if(TryComp<InventoryComponent>(bodyId, out var inventory))
if (TryComp<InventoryComponent>(bodyId, out var inventory))
{
foreach (var item in _inventory.GetHandOrInventoryEntities(bodyId))
{

View File

@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Body.Components;
using Content.Shared.Body.Events;
using Content.Shared.Body.Organ;
@@ -9,41 +9,50 @@ namespace Content.Shared.Body.Systems;
public partial class SharedBodySystem
{
private void AddOrgan(EntityUid uid, EntityUid bodyUid, EntityUid parentPartUid, OrganComponent component)
private void AddOrgan(
Entity<OrganComponent> organEnt,
EntityUid bodyUid,
EntityUid parentPartUid)
{
component.Body = bodyUid;
RaiseLocalEvent(uid, new AddedToPartEvent(parentPartUid));
organEnt.Comp.Body = bodyUid;
var addedEv = new OrganAddedEvent(parentPartUid);
RaiseLocalEvent(organEnt, ref addedEv);
if (component.Body != null)
RaiseLocalEvent(uid, new AddedToPartInBodyEvent(component.Body.Value, parentPartUid));
Dirty(uid, component);
}
private void RemoveOrgan(EntityUid uid, EntityUid parentPartUid, OrganComponent component)
{
RaiseLocalEvent(uid, new RemovedFromPartEvent(parentPartUid));
if (component.Body != null)
if (organEnt.Comp.Body is not null)
{
RaiseLocalEvent(uid, new RemovedFromPartInBodyEvent(component.Body.Value, parentPartUid));
var addedInBodyEv = new OrganAddedToBodyEvent(bodyUid, parentPartUid);
RaiseLocalEvent(organEnt, ref addedInBodyEv);
}
component.Body = null;
Dirty(uid, component);
Dirty(organEnt, organEnt.Comp);
}
private void RemoveOrgan(Entity<OrganComponent> organEnt, EntityUid parentPartUid)
{
var removedEv = new OrganRemovedEvent(parentPartUid);
RaiseLocalEvent(organEnt, ref removedEv);
if (organEnt.Comp.Body is { Valid: true } bodyUid)
{
var removedInBodyEv = new OrganRemovedFromBodyEvent(bodyUid, parentPartUid);
RaiseLocalEvent(organEnt, ref removedInBodyEv);
}
organEnt.Comp.Body = null;
Dirty(organEnt, organEnt.Comp);
}
/// <summary>
/// Creates the specified organ slot on the parent entity.
/// </summary>
private OrganSlot? CreateOrganSlot(string slotId, EntityUid parent, BodyPartComponent? part = null)
private OrganSlot? CreateOrganSlot(Entity<BodyPartComponent?> parentEnt, string slotId)
{
if (!Resolve(parent, ref part, false))
if (!Resolve(parentEnt, ref parentEnt.Comp, logMissing: false))
return null;
Containers.EnsureContainer<ContainerSlot>(parent, GetOrganContainerId(slotId));
Containers.EnsureContainer<ContainerSlot>(parentEnt, GetOrganContainerId(slotId));
var slot = new OrganSlot(slotId);
part.Organs.Add(slotId, slot);
parentEnt.Comp.Organs.Add(slotId, slot);
return slot;
}
@@ -58,20 +67,23 @@ public partial class SharedBodySystem
{
slot = null;
if (parent == null || !Resolve(parent.Value, ref part, false))
if (parent is null || !Resolve(parent.Value, ref part, logMissing: false))
{
return false;
}
Containers.EnsureContainer<ContainerSlot>(parent.Value, GetOrganContainerId(slotId));
slot = new OrganSlot(slotId);
return part.Organs.TryAdd(slotId,slot.Value);
return part.Organs.TryAdd(slotId, slot.Value);
}
/// <summary>
/// Returns whether the slotId exists on the partId.
/// </summary>
public bool CanInsertOrgan(EntityUid partId, string slotId, BodyPartComponent? part = null)
public bool CanInsertOrgan(
EntityUid partId,
string slotId,
BodyPartComponent? part = null)
{
return Resolve(partId, ref part) && part.Organs.ContainsKey(slotId);
}
@@ -79,26 +91,32 @@ public partial class SharedBodySystem
/// <summary>
/// Returns whether the specified organ slot exists on the partId.
/// </summary>
public bool CanInsertOrgan(EntityUid partId, OrganSlot slot, BodyPartComponent? part = null)
public bool CanInsertOrgan(
EntityUid partId,
OrganSlot slot,
BodyPartComponent? part = null)
{
return CanInsertOrgan(partId, slot.Id, part);
}
public bool InsertOrgan(EntityUid partId, EntityUid organId, string slotId, BodyPartComponent? part = null, OrganComponent? organ = null)
public bool InsertOrgan(
EntityUid partId,
EntityUid organId,
string slotId,
BodyPartComponent? part = null,
OrganComponent? organ = null)
{
if (!Resolve(organId, ref organ, false) ||
!Resolve(partId, ref part, false) ||
!CanInsertOrgan(partId, slotId, part))
if (!Resolve(organId, ref organ, logMissing: false)
|| !Resolve(partId, ref part, logMissing: false)
|| !CanInsertOrgan(partId, slotId, part))
{
return false;
}
var containerId = GetOrganContainerId(slotId);
if (!Containers.TryGetContainer(partId, containerId, out var container))
return false;
return Containers.Insert(organId, container);
return Containers.TryGetContainer(partId, containerId, out var container)
&& Containers.Insert(organId, container);
}
/// <summary>
@@ -111,10 +129,8 @@ public partial class SharedBodySystem
var parent = container.Owner;
if (!HasComp<BodyPartComponent>(parent))
return false;
return Containers.Remove(organId, container);
return HasComp<BodyPartComponent>(parent)
&& Containers.Remove(organId, container);
}
/// <summary>
@@ -126,8 +142,8 @@ public partial class SharedBodySystem
BodyPartComponent? part = null,
OrganComponent? organ = null)
{
if (!Resolve(partId, ref part, false) ||
!Resolve(organId, ref organ, false))
if (!Resolve(partId, ref part, logMissing: false)
|| !Resolve(organId, ref organ, logMissing: false))
{
return false;
}

View File

@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Body.Components;
using Content.Shared.Body.Events;
@@ -23,52 +23,52 @@ public partial class SharedBodySystem
SubscribeLocalEvent<BodyPartComponent, EntRemovedFromContainerMessage>(OnBodyPartRemoved);
}
private void OnBodyPartInserted(EntityUid uid, BodyPartComponent component, EntInsertedIntoContainerMessage args)
private void OnBodyPartInserted(Entity<BodyPartComponent> ent, ref EntInsertedIntoContainerMessage args)
{
// Body part inserted into another body part.
var entity = args.Entity;
var insertedUid = args.Entity;
var slotId = args.Container.ID;
if (component.Body == null)
if (ent.Comp.Body is null)
return;
if (TryComp(entity, out BodyPartComponent? childPart))
if (TryComp(insertedUid, out BodyPartComponent? part))
{
AddPart(component.Body.Value, entity, slotId, childPart);
RecursiveBodyUpdate(entity, component.Body.Value, childPart);
AddPart(ent.Comp.Body.Value, (insertedUid, part), slotId);
RecursiveBodyUpdate((insertedUid, part), ent.Comp.Body.Value);
}
if (TryComp(entity, out OrganComponent? organ))
AddOrgan(entity, component.Body.Value, uid, organ);
if (TryComp(insertedUid, out OrganComponent? organ))
AddOrgan((insertedUid, organ), ent.Comp.Body.Value, ent);
}
private void OnBodyPartRemoved(EntityUid uid, BodyPartComponent component, EntRemovedFromContainerMessage args)
private void OnBodyPartRemoved(Entity<BodyPartComponent> ent, ref EntRemovedFromContainerMessage args)
{
// Body part removed from another body part.
var entity = args.Entity;
var removedUid = args.Entity;
var slotId = args.Container.ID;
DebugTools.Assert(!TryComp(entity, out BodyPartComponent? b) || b.Body == component.Body);
DebugTools.Assert(!TryComp(entity, out OrganComponent? o) || o.Body == component.Body);
DebugTools.Assert(!TryComp(removedUid, out BodyPartComponent? b) || b.Body == ent.Comp.Body);
DebugTools.Assert(!TryComp(removedUid, out OrganComponent? o) || o.Body == ent.Comp.Body);
if (TryComp(entity, out BodyPartComponent? childPart) && childPart.Body != null)
if (TryComp(removedUid, out BodyPartComponent? part) && part.Body is not null)
{
RemovePart(childPart.Body.Value, entity, slotId, childPart);
RecursiveBodyUpdate(entity, null, childPart);
RemovePart(part.Body.Value, (removedUid, part), slotId);
RecursiveBodyUpdate((removedUid, part), null);
}
if (TryComp(entity, out OrganComponent? organ))
RemoveOrgan(entity, uid, organ);
if (TryComp(removedUid, out OrganComponent? organ))
RemoveOrgan((removedUid, organ), ent);
}
private void RecursiveBodyUpdate(EntityUid uid, EntityUid? bodyUid, BodyPartComponent component)
private void RecursiveBodyUpdate(Entity<BodyPartComponent> ent, EntityUid? bodyUid)
{
component.Body = bodyUid;
Dirty(uid, component);
ent.Comp.Body = bodyUid;
Dirty(ent, ent.Comp);
foreach (var slotId in component.Organs.Keys)
foreach (var slotId in ent.Comp.Organs.Keys)
{
if (!Containers.TryGetContainer(uid, GetOrganContainerId(slotId), out var container))
if (!Containers.TryGetContainer(ent, GetOrganContainerId(slotId), out var container))
continue;
foreach (var organ in container.ContainedEntities)
@@ -78,105 +78,108 @@ public partial class SharedBodySystem
Dirty(organ, organComp);
if (organComp.Body != null)
RaiseLocalEvent(organ, new RemovedFromPartInBodyEvent(organComp.Body.Value, uid));
if (organComp.Body is { Valid: true } oldBodyUid)
{
var removedEv = new OrganRemovedFromBodyEvent(oldBodyUid, ent);
RaiseLocalEvent(organ, ref removedEv);
}
organComp.Body = bodyUid;
if (bodyUid != null)
RaiseLocalEvent(organ, new AddedToPartInBodyEvent(bodyUid.Value, uid));
if (bodyUid is not null)
{
var addedEv = new OrganAddedToBodyEvent(bodyUid.Value, ent);
RaiseLocalEvent(organ, ref addedEv);
}
}
}
foreach (var slotId in component.Children.Keys)
foreach (var slotId in ent.Comp.Children.Keys)
{
if (!Containers.TryGetContainer(uid, GetPartSlotContainerId(slotId), out var container))
if (!Containers.TryGetContainer(ent, GetPartSlotContainerId(slotId), out var container))
continue;
foreach (var containedEnt in container.ContainedEntities)
foreach (var containedUid in container.ContainedEntities)
{
if (TryComp(containedEnt, out BodyPartComponent? childPart))
RecursiveBodyUpdate(containedEnt, bodyUid, childPart);
if (TryComp(containedUid, out BodyPartComponent? childPart))
RecursiveBodyUpdate((containedUid, childPart), bodyUid);
}
}
}
protected virtual void AddPart(
EntityUid bodyUid,
EntityUid partUid,
string slotId,
BodyPartComponent component,
BodyComponent? bodyComp = null)
Entity<BodyComponent?> bodyEnt,
Entity<BodyPartComponent> partEnt,
string slotId)
{
DebugTools.AssertOwner(partUid, component);
Dirty(partUid, component);
component.Body = bodyUid;
Dirty(partEnt, partEnt.Comp);
partEnt.Comp.Body = bodyEnt;
var ev = new BodyPartAddedEvent(slotId, component);
RaiseLocalEvent(bodyUid, ref ev);
var ev = new BodyPartAddedEvent(slotId, partEnt);
RaiseLocalEvent(bodyEnt, ref ev);
AddLeg(partUid, bodyUid, component, bodyComp);
AddLeg(partEnt, bodyEnt);
}
protected virtual void RemovePart(
EntityUid bodyUid,
EntityUid partUid,
string slotId,
BodyPartComponent component,
BodyComponent? bodyComp = null)
Entity<BodyComponent?> bodyEnt,
Entity<BodyPartComponent> partEnt,
string slotId)
{
DebugTools.AssertOwner(partUid, component);
Resolve(bodyUid, ref bodyComp, false);
Dirty(partUid, component);
component.Body = null;
Resolve(bodyEnt, ref bodyEnt.Comp, logMissing: false);
Dirty(partEnt, partEnt.Comp);
partEnt.Comp.Body = null;
var ev = new BodyPartRemovedEvent(slotId, component);
RaiseLocalEvent(bodyUid, ref ev);
var ev = new BodyPartRemovedEvent(slotId, partEnt);
RaiseLocalEvent(bodyEnt, ref ev);
RemoveLeg(partUid, bodyUid, component);
PartRemoveDamage(bodyUid, component, bodyComp);
RemoveLeg(partEnt, bodyEnt);
PartRemoveDamage(bodyEnt, partEnt);
}
private void AddLeg(EntityUid uid, EntityUid bodyUid, BodyPartComponent component, BodyComponent? bodyComp = null)
private void AddLeg(Entity<BodyPartComponent> legEnt, Entity<BodyComponent?> bodyEnt)
{
if (!Resolve(bodyUid, ref bodyComp, false))
if (!Resolve(bodyEnt, ref bodyEnt.Comp, logMissing: false))
return;
if (component.PartType == BodyPartType.Leg)
if (legEnt.Comp.PartType == BodyPartType.Leg)
{
bodyComp.LegEntities.Add(uid);
UpdateMovementSpeed(bodyUid);
Dirty(bodyUid, bodyComp);
bodyEnt.Comp.LegEntities.Add(legEnt);
UpdateMovementSpeed(bodyEnt);
Dirty(bodyEnt, bodyEnt.Comp);
}
}
private void RemoveLeg(EntityUid uid, EntityUid bodyUid, BodyPartComponent component, BodyComponent? bodyComp = null)
private void RemoveLeg(Entity<BodyPartComponent> legEnt, Entity<BodyComponent?> bodyEnt)
{
if (!Resolve(bodyUid, ref bodyComp, false))
if (!Resolve(bodyEnt, ref bodyEnt.Comp, logMissing: false))
return;
if (component.PartType == BodyPartType.Leg)
if (legEnt.Comp.PartType == BodyPartType.Leg)
{
bodyComp.LegEntities.Remove(uid);
UpdateMovementSpeed(bodyUid);
Dirty(bodyUid, bodyComp);
bodyEnt.Comp.LegEntities.Remove(legEnt);
UpdateMovementSpeed(bodyEnt);
Dirty(bodyEnt, bodyEnt.Comp);
if (!bodyComp.LegEntities.Any())
if (!bodyEnt.Comp.LegEntities.Any())
{
Standing.Down(bodyUid);
Standing.Down(bodyEnt);
}
}
}
private void PartRemoveDamage(EntityUid parent, BodyPartComponent component, BodyComponent? bodyComp = null)
private void PartRemoveDamage(Entity<BodyComponent?> bodyEnt, Entity<BodyPartComponent> partEnt)
{
if (!Resolve(parent, ref bodyComp, false))
if (!Resolve(bodyEnt, ref bodyEnt.Comp, logMissing: false))
return;
if (!_timing.ApplyingState && component.IsVital && !GetBodyChildrenOfType(parent, component.PartType, bodyComp).Any())
if (!_timing.ApplyingState
&& partEnt.Comp.IsVital
&& !GetBodyChildrenOfType(bodyEnt, partEnt.Comp.PartType, bodyEnt.Comp).Any()
)
{
// TODO BODY SYSTEM KILL : remove this when wounding and required parts are implemented properly
var damage = new DamageSpecifier(Prototypes.Index<DamageTypePrototype>("Bloodloss"), 300);
Damageable.TryChangeDamage(parent, damage);
Damageable.TryChangeDamage(bodyEnt, damage);
}
}
@@ -212,7 +215,8 @@ public partial class SharedBodySystem
var parent = container.Owner;
if (!TryComp<BodyPartComponent>(parent, out var parentBody) || !parentBody.Children.ContainsKey(slotId))
if (!TryComp<BodyPartComponent>(parent, out var parentBody)
|| !parentBody.Children.ContainsKey(slotId))
return null;
return (parent, slotId);
@@ -252,7 +256,7 @@ public partial class SharedBodySystem
BodyPartType partType,
BodyPartComponent? part = null)
{
if (!Resolve(partUid, ref part, false))
if (!Resolve(partUid, ref part, logMissing: false))
return null;
Containers.EnsureContainer<ContainerSlot>(partUid, GetPartSlotContainerId(slotId));
@@ -275,8 +279,8 @@ public partial class SharedBodySystem
{
slot = null;
if (partId == null ||
!Resolve(partId.Value, ref part, false))
if (partId is null
|| !Resolve(partId.Value, ref part, logMissing: false))
{
return false;
}
@@ -310,24 +314,31 @@ public partial class SharedBodySystem
/// <summary>
/// Returns true if the partId is the root body container for the specified bodyId.
/// </summary>
public bool IsPartRoot(EntityUid bodyId, EntityUid partId, BodyComponent? body = null, BodyPartComponent? part = null)
public bool IsPartRoot(
EntityUid bodyId,
EntityUid partId,
BodyComponent? body = null,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part)|| !Resolve(bodyId, ref body))
return false;
return Containers.TryGetContainingContainer(bodyId, partId, out var container) && container.ID == BodyRootContainerId;
return Resolve(partId, ref part)
&& Resolve(bodyId, ref body)
&& Containers.TryGetContainingContainer(bodyId, partId, out var container)
&& container.ID == BodyRootContainerId;
}
/// <summary>
/// Returns true if we can attach the partId to the bodyId as the root entity.
/// </summary>
public bool CanAttachToRoot(EntityUid bodyId, EntityUid partId, BodyComponent? body = null,
public bool CanAttachToRoot(
EntityUid bodyId,
EntityUid partId,
BodyComponent? body = null,
BodyPartComponent? part = null)
{
return Resolve(bodyId, ref body) &&
Resolve(partId, ref part) &&
body.RootContainer.ContainedEntity == null &&
bodyId != part.Body;
return Resolve(bodyId, ref body)
&& Resolve(partId, ref part)
&& body.RootContainer.ContainedEntity is null
&& bodyId != part.Body;
}
/// <summary>
@@ -335,8 +346,11 @@ public partial class SharedBodySystem
/// </summary>
public (EntityUid Entity, BodyPartComponent BodyPart)? GetRootPartOrNull(EntityUid bodyId, BodyComponent? body = null)
{
if (!Resolve(bodyId, ref body) || body.RootContainer.ContainedEntity == null)
if (!Resolve(bodyId, ref body)
|| body.RootContainer.ContainedEntity is null)
{
return null;
}
return (body.RootContainer.ContainedEntity.Value,
Comp<BodyPartComponent>(body.RootContainer.ContainedEntity.Value));
@@ -352,13 +366,9 @@ public partial class SharedBodySystem
BodyPartComponent? parentPart = null,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false) ||
!Resolve(parentId, ref parentPart, false))
{
return false;
}
return CanAttachPart(parentId, slot.Id, partId, parentPart, part);
return Resolve(partId, ref part, logMissing: false)
&& Resolve(parentId, ref parentPart, logMissing: false)
&& CanAttachPart(parentId, slot.Id, partId, parentPart, part);
}
/// <summary>
@@ -371,16 +381,12 @@ public partial class SharedBodySystem
BodyPartComponent? parentPart = null,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false) ||
!Resolve(parentId, ref parentPart, false) ||
!parentPart.Children.TryGetValue(slotId, out var parentSlotData))
{
return false;
}
return part.PartType == parentSlotData.Type &&
Containers.TryGetContainer(parentId, GetPartSlotContainerId(slotId), out var container) &&
Containers.CanInsert(partId, container);
return Resolve(partId, ref part, logMissing: false)
&& Resolve(parentId, ref parentPart, logMissing: false)
&& parentPart.Children.TryGetValue(slotId, out var parentSlotData)
&& part.PartType == parentSlotData.Type
&& Containers.TryGetContainer(parentId, GetPartSlotContainerId(slotId), out var container)
&& Containers.CanInsert(partId, container);
}
public bool AttachPartToRoot(
@@ -389,14 +395,10 @@ public partial class SharedBodySystem
BodyComponent? body = null,
BodyPartComponent? part = null)
{
if (!Resolve(bodyId, ref body) ||
!Resolve(partId, ref part) ||
!CanAttachToRoot(bodyId, partId, body, part))
{
return false;
}
return Containers.Insert(partId, body.RootContainer);
return Resolve(bodyId, ref body)
&& Resolve(partId, ref part)
&& CanAttachToRoot(bodyId, partId, body, part)
&& Containers.Insert(partId, body.RootContainer);
}
#endregion
@@ -406,20 +408,16 @@ public partial class SharedBodySystem
/// <summary>
/// Attaches a body part to the specified body part parent.
/// </summary>
public bool AttachPart(
EntityUid parentPartId,
string slotId,
EntityUid partId,
BodyPartComponent? parentPart = null,
BodyPartComponent? part = null)
public bool AttachPart(
EntityUid parentPartId,
string slotId,
EntityUid partId,
BodyPartComponent? parentPart = null,
BodyPartComponent? part = null)
{
if (!Resolve(parentPartId, ref parentPart, false) ||
!parentPart.Children.TryGetValue(slotId, out var slot))
{
return false;
}
return AttachPart(parentPartId, slot, partId, parentPart, part);
return Resolve(parentPartId, ref parentPart, logMissing: false)
&& parentPart.Children.TryGetValue(slotId, out var slot)
&& AttachPart(parentPartId, slot, partId, parentPart, part);
}
/// <summary>
@@ -432,10 +430,10 @@ public partial class SharedBodySystem
BodyPartComponent? parentPart = null,
BodyPartComponent? part = null)
{
if (!Resolve(parentPartId, ref parentPart, false) ||
!Resolve(partId, ref part, false) ||
!CanAttachPart(parentPartId, slot.Id, partId, parentPart, part) ||
!parentPart.Children.ContainsKey(slot.Id))
if (!Resolve(parentPartId, ref parentPart, logMissing: false)
|| !Resolve(partId, ref part, logMissing: false)
|| !CanAttachPart(parentPartId, slot.Id, partId, parentPart, part)
|| !parentPart.Children.ContainsKey(slot.Id))
{
return false;
}
@@ -453,13 +451,16 @@ public partial class SharedBodySystem
#region Misc
public void UpdateMovementSpeed(EntityUid bodyId, BodyComponent? body = null, MovementSpeedModifierComponent? movement = null)
public void UpdateMovementSpeed(
EntityUid bodyId,
BodyComponent? body = null,
MovementSpeedModifierComponent? movement = null)
{
if (!Resolve(bodyId, ref body, ref movement, false))
return;
if (body.RequiredLegs <= 0)
if (!Resolve(bodyId, ref body, ref movement, logMissing: false)
|| body.RequiredLegs <= 0)
{
return;
}
var walkSpeed = 0f;
var sprintSpeed = 0f;
@@ -488,7 +489,7 @@ public partial class SharedBodySystem
/// </summary>
public IEnumerable<(EntityUid Id, OrganComponent Component)> GetPartOrgans(EntityUid partId, BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
yield break;
foreach (var slotId in part.Organs.Keys)
@@ -513,7 +514,7 @@ public partial class SharedBodySystem
/// </summary>
public IEnumerable<BaseContainer> GetPartContainers(EntityUid id, BodyPartComponent? part = null)
{
if (!Resolve(id, ref part, false) ||
if (!Resolve(id, ref part, logMissing: false) ||
part.Children.Count == 0)
{
yield break;
@@ -541,9 +542,11 @@ public partial class SharedBodySystem
/// <summary>
/// Returns all body part components for this entity including itself.
/// </summary>
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyPartChildren(EntityUid partId, BodyPartComponent? part = null)
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyPartChildren(
EntityUid partId,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
yield break;
yield return (partId, part);
@@ -571,9 +574,11 @@ public partial class SharedBodySystem
/// <summary>
/// Returns all body part slots for this entity.
/// </summary>
public IEnumerable<BodyPartSlot> GetAllBodyPartSlots(EntityUid partId, BodyPartComponent? part = null)
public IEnumerable<BodyPartSlot> GetAllBodyPartSlots(
EntityUid partId,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
yield break;
foreach (var (slotId, slot) in part.Children)
@@ -601,7 +606,10 @@ public partial class SharedBodySystem
/// <summary>
/// Returns true if the bodyId has any parts of this type.
/// </summary>
public bool BodyHasPartType(EntityUid bodyId, BodyPartType type, BodyComponent? body = null)
public bool BodyHasPartType(
EntityUid bodyId,
BodyPartType type,
BodyComponent? body = null)
{
return GetBodyChildrenOfType(bodyId, type, body).Any();
}
@@ -615,8 +623,8 @@ public partial class SharedBodySystem
BodyPartComponent? parent,
BodyPartComponent? child)
{
if (!Resolve(parentId, ref parent, false) ||
!Resolve(childId, ref child, false))
if (!Resolve(parentId, ref parent, logMissing: false)
|| !Resolve(childId, ref child, logMissing: false))
{
return false;
}
@@ -638,15 +646,11 @@ public partial class SharedBodySystem
BodyComponent? body = null,
BodyPartComponent? part = null)
{
if (!Resolve(bodyId, ref body, false) ||
body.RootContainer.ContainedEntity == null ||
!Resolve(partId, ref part, false) ||
!TryComp(body.RootContainer.ContainedEntity, out BodyPartComponent? rootPart))
{
return false;
}
return PartHasChild(body.RootContainer.ContainedEntity.Value, partId, rootPart, part);
return Resolve(bodyId, ref body, logMissing: false)
&& body.RootContainer.ContainedEntity is not null
&& Resolve(partId, ref part, logMissing: false)
&& TryComp(body.RootContainer.ContainedEntity, out BodyPartComponent? rootPart)
&& PartHasChild(body.RootContainer.ContainedEntity.Value, partId, rootPart, part);
}
public IEnumerable<(EntityUid Id, BodyPartComponent Component)> GetBodyChildrenOfType(
@@ -721,9 +725,11 @@ public partial class SharedBodySystem
/// <summary>
/// Gets the parent body part and all immediate child body parts for the partId.
/// </summary>
public IEnumerable<EntityUid> GetBodyPartAdjacentParts(EntityUid partId, BodyPartComponent? part = null)
public IEnumerable<EntityUid> GetBodyPartAdjacentParts(
EntityUid partId,
BodyPartComponent? part = null)
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
yield break;
if (TryGetParentBodyPart(partId, out var parentUid, out _))
@@ -745,7 +751,7 @@ public partial class SharedBodySystem
BodyPartComponent? part = null)
where T : IComponent
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
yield break;
var query = GetEntityQuery<T>();
@@ -762,7 +768,7 @@ public partial class SharedBodySystem
BodyPartComponent? part = null)
where T : IComponent
{
if (!Resolve(partId, ref part, false))
if (!Resolve(partId, ref part, logMissing: false))
{
comps = null;
return false;