using System.Diagnostics.CodeAnalysis; using Content.Shared.Machines.Components; namespace Content.Shared.Machines.EntitySystems; /// /// Shared handling of multipart machines. /// public abstract class SharedMultipartMachineSystem : EntitySystem { protected EntityQuery XformQuery; public override void Initialize() { base.Initialize(); XformQuery = GetEntityQuery(); } /// /// Returns whether each non-optional part of the machine has a matched entity /// /// Entity to check the assembled state of. /// True if all non-optional parts have a matching entity, false otherwise. public bool IsAssembled(Entity ent) { if (!Resolve(ent, ref ent.Comp)) return false; foreach (var part in ent.Comp.Parts.Values) { if (!part.Entity.HasValue && !part.Optional) return false; } return true; } /// /// Returns whether a machine has a specifed EntityUid bound to one of its parts. /// /// Entity, which might have a multpart machine attached, to use for the query. /// EntityUid to search for. /// True if any part has the specified EntityUid, false otherwise. public bool HasPartEntity(Entity machine, EntityUid entity) { if (!Resolve(machine, ref machine.Comp)) return false; foreach (var part in machine.Comp.Parts.Values) { if (part.Entity.HasValue && part.Entity.Value == entity) return true; } return false; } /// /// Get the EntityUid for the entity bound to a specific part, if one exists. /// /// Entity, which might have a multipart machine attached, to use for the query. /// Enum value for the part to find, must match the value specified in YAML. /// May contain the resolved EntityUid for the specified part, null otherwise. public EntityUid? GetPartEntity(Entity ent, Enum part) { if (!TryGetPartEntity(ent, part, out var entity)) return null; return entity; } /// /// Get the EntityUid for the entity bound to a specific part, if one exists. /// /// Entity, which might have a multipart machine attached, to use for the query. /// Enum for the part to find, must match the value specified in YAML. /// Out var which may contain the matched EntityUid for the specified part. /// True if the part is found and has a matched entity, false otherwise. public bool TryGetPartEntity( Entity ent, Enum part, [NotNullWhen(true)] out EntityUid? entity ) { entity = null; if (!Resolve(ent, ref ent.Comp)) return false; if (ent.Comp.Parts.TryGetValue(part, out var value) && value.Entity.HasValue) { entity = value.Entity.Value; return true; } return false; } /// /// Check if a machine has an entity bound to a specific part /// /// Entity, which might have a multipart machine attached, to use for the query. /// Enum for the part to find. /// True if the specific part has a entity bound to it, false otherwise. public bool HasPart(Entity ent, Enum part) { if (!Resolve(ent, ref ent.Comp)) return false; if (!ent.Comp.Parts.TryGetValue(part, out var value)) return false; return value.Entity != null; } }