Implanters and Subdermal Implants (#11840)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
keronshb
2022-11-20 01:49:37 -05:00
committed by GitHub
parent a5dff7eee7
commit 671324bef8
48 changed files with 1633 additions and 79 deletions

View File

@@ -162,7 +162,7 @@ public partial class SharedBodySystem
}
public virtual HashSet<EntityUid> GibBody(EntityUid? partId, bool gibOrgans = false,
BodyComponent? body = null)
BodyComponent? body = null, bool deleteItems = false)
{
if (partId == null || !Resolve(partId.Value, ref body, false))
return new HashSet<EntityUid>();

View File

@@ -0,0 +1,13 @@
using Robust.Shared.Containers;
namespace Content.Shared.Implants.Components;
/// <summary>
/// Added to an entity via the <see cref="SharedImplanterSystem"/> on implant
/// Used in instances where mob info needs to be passed to the implant such as MobState triggers
/// </summary>
[RegisterComponent]
public sealed class ImplantedComponent : Component
{
public Container ImplantContainer = default!;
}

View File

@@ -0,0 +1,107 @@
using System.Threading;
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Implants.Components;
/// <summary>
/// Implanters are used to implant or extract implants from an entity
/// Some can be single use (implant only) or some can draw out an implant
/// </summary>
//TODO: Rework drawing to work with implant cases when surgery is in
[RegisterComponent, NetworkedComponent]
public sealed class ImplanterComponent : Component
{
public const string ImplanterSlotId = "implanter_slot";
public const string ImplantSlotId = "implant";
/// <summary>
/// Used for implanters that start with specific implants
/// </summary>
[ViewVariables]
[DataField("implant", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string? Implant;
/// <summary>
/// The time it takes to implant someone else
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("implantTime")]
public float ImplantTime = 5f;
//TODO: Remove when surgery is a thing
/// <summary>
/// The time it takes to extract an implant from someone
/// It's excessively long to deter from implant checking any antag
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("drawTime")]
public float DrawTime = 300f;
/// <summary>
/// Good for single-use injectors
/// </summary>
[ViewVariables]
[DataField("implantOnly")]
public bool ImplantOnly = false;
/// <summary>
/// The current mode of the implanter
/// Mode is changed automatically depending if it implants or draws
/// </summary>
[ViewVariables]
[DataField("currentMode")]
public ImplanterToggleMode CurrentMode;
/// <summary>
/// The name and description of the implant to show on the implanter
/// </summary>
[ViewVariables]
[DataField("implantData")]
public (string, string) ImplantData;
/// <summary>
/// The <see cref="ItemSlot"/> for this implanter
/// </summary>
[ViewVariables]
[DataField("implanterSlot")]
public ItemSlot ImplanterSlot = new();
public bool UiUpdateNeeded;
public CancellationTokenSource? CancelToken;
}
[Serializable, NetSerializable]
public sealed class ImplanterComponentState : ComponentState
{
public ImplanterToggleMode CurrentMode;
public bool ImplantOnly;
public ImplanterComponentState(ImplanterToggleMode currentMode, bool implantOnly)
{
CurrentMode = currentMode;
ImplantOnly = implantOnly;
}
}
[Serializable, NetSerializable]
public enum ImplanterToggleMode : byte
{
Inject,
Draw
}
[Serializable, NetSerializable]
public enum ImplanterVisuals : byte
{
Full
}
[Serializable, NetSerializable]
public enum ImplanterImplantOnlyVisuals : byte
{
ImplantOnly
}

View File

@@ -0,0 +1,53 @@
using Content.Shared.Actions;
namespace Content.Shared.Implants.Components;
/// <summary>
/// Subdermal implants get stored in a container on an entity and grant the entity special actions
/// The actions can be activated via an action, a passive ability (ie tracking), or a reactive ability (ie on death) or some sort of combination
/// They're added and removed with implanters
/// </summary>
[RegisterComponent]
public sealed class SubdermalImplantComponent : Component
{
/// <summary>
/// Used where you want the implant to grant the owner an instant action.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("implantAction")]
public string? ImplantAction;
/// <summary>
/// The entity this implant is inside
/// </summary>
[ViewVariables]
public EntityUid? ImplantedEntity;
/// <summary>
/// Should this implant be removeable?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("permanent")]
public bool Permanent = false;
}
/// <summary>
/// Used for opening the storage implant via action.
/// </summary>
public sealed class OpenStorageImplantEvent : InstantActionEvent
{
}
public sealed class UseFreedomImplantEvent : InstantActionEvent
{
}
/// <summary>
/// Used for triggering trigger events on the implant via action
/// </summary>
public sealed class ActivateImplantEvent : InstantActionEvent
{
}

View File

@@ -0,0 +1,9 @@
namespace Content.Shared.Implants.Components;
/// <summary>
/// Triggers implants when the action is pressed
/// </summary>
[RegisterComponent]
public sealed class TriggerImplantActionComponent : Component
{
}

View File

@@ -0,0 +1,156 @@
using System.Linq;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.IdentityManagement;
using Content.Shared.Implants.Components;
using Content.Shared.Popups;
using Robust.Shared.Containers;
using Robust.Shared.Player;
namespace Content.Shared.Implants;
public abstract class SharedImplanterSystem : EntitySystem
{
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ImplanterComponent, ComponentInit>(OnImplanterInit);
SubscribeLocalEvent<ImplanterComponent, EntInsertedIntoContainerMessage>(OnEntInserted);
}
private void OnImplanterInit(EntityUid uid, ImplanterComponent component, ComponentInit args)
{
if (component.Implant != null)
component.ImplanterSlot.StartingItem = component.Implant;
_itemSlots.AddItemSlot(uid, ImplanterComponent.ImplanterSlotId, component.ImplanterSlot);
}
private void OnEntInserted(EntityUid uid, ImplanterComponent component, EntInsertedIntoContainerMessage args)
{
var implantData = EntityManager.GetComponent<MetaDataComponent>(args.Entity);
component.ImplantData = (implantData.EntityName, implantData.EntityDescription);
}
//Instantly implant something and add all necessary components and containers.
//Set to draw mode if not implant only
public void Implant(EntityUid implanter, EntityUid target, ImplanterComponent component)
{
var implanterContainer = component.ImplanterSlot.ContainerSlot;
if (implanterContainer is null)
return;
var implant = implanterContainer.ContainedEntities.FirstOrDefault();
if (!TryComp<SubdermalImplantComponent>(implant, out var implantComp))
return;
//If the target doesn't have the implanted component, add it.
var implantedComp = EnsureComp<ImplantedComponent>(target);
var implantContainer = implantedComp.ImplantContainer;
implanterContainer.Remove(implant);
implantComp.ImplantedEntity = target;
implantContainer.OccludesLight = false;
implantContainer.Insert(implant);
if (component.CurrentMode == ImplanterToggleMode.Inject && !component.ImplantOnly)
DrawMode(component);
else
ImplantMode(component);
Dirty(component);
}
//Draw the implant out of the target
//TODO: Rework when surgery is in so implant cases can be a thing
public void Draw(EntityUid implanter, EntityUid user, EntityUid target, ImplanterComponent component)
{
var implanterContainer = component.ImplanterSlot.ContainerSlot;
if (implanterContainer is null)
return;
var permanentFound = false;
if (_container.TryGetContainer(target, ImplanterComponent.ImplantSlotId, out var implantContainer))
{
var implantCompQuery = GetEntityQuery<SubdermalImplantComponent>();
foreach (var implant in implantContainer.ContainedEntities)
{
if (!implantCompQuery.TryGetComponent(implant, out var implantComp))
return;
//Don't remove a permanent implant and look for the next that can be drawn
if (!implantContainer.CanRemove(implant))
{
var implantName = Identity.Entity(implant, EntityManager);
var targetName = Identity.Entity(target, EntityManager);
var failedPermanentMessage = Loc.GetString("implanter-draw-failed-permanent", ("implant", implantName), ("target", targetName));
_popup.PopupEntity(failedPermanentMessage, target, Filter.Entities(user));
permanentFound = implantComp.Permanent;
continue;
}
implantContainer.Remove(implant);
implantComp.ImplantedEntity = null;
implanterContainer.Insert(implant);
permanentFound = implantComp.Permanent;
//Break so only one implant is drawn
break;
}
if (component.CurrentMode == ImplanterToggleMode.Draw && !component.ImplantOnly && !permanentFound)
ImplantMode(component);
Dirty(component);
}
}
private void ImplantMode(ImplanterComponent component)
{
component.CurrentMode = ImplanterToggleMode.Inject;
ChangeOnImplantVisualizer(component);
}
private void DrawMode(ImplanterComponent component)
{
component.CurrentMode = ImplanterToggleMode.Draw;
ChangeOnImplantVisualizer(component);
}
private void ChangeOnImplantVisualizer(ImplanterComponent component)
{
if (!TryComp<AppearanceComponent>(component.Owner, out var appearance))
return;
bool implantFound;
if (component.ImplanterSlot.HasItem)
implantFound = true;
else
implantFound = false;
if (component.CurrentMode == ImplanterToggleMode.Inject && !component.ImplantOnly)
_appearance.SetData(component.Owner, ImplanterVisuals.Full, implantFound, appearance);
else if (component.CurrentMode == ImplanterToggleMode.Inject && component.ImplantOnly)
{
_appearance.SetData(component.Owner, ImplanterVisuals.Full, implantFound, appearance);
_appearance.SetData(component.Owner, ImplanterImplantOnlyVisuals.ImplantOnly, component.ImplantOnly, appearance);
}
else
_appearance.SetData(component.Owner, ImplanterVisuals.Full, implantFound, appearance);
}
}

View File

@@ -0,0 +1,120 @@
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Implants.Components;
using Content.Shared.Tag;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
namespace Content.Shared.Implants;
public abstract class SharedSubdermalImplantSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly TagSystem _tag = default!;
public const string BaseStorageId = "storagebase";
public override void Initialize()
{
SubscribeLocalEvent<SubdermalImplantComponent, EntGotInsertedIntoContainerMessage>(OnInsert);
SubscribeLocalEvent<SubdermalImplantComponent, ContainerGettingRemovedAttemptEvent>(OnRemoveAttempt);
SubscribeLocalEvent<SubdermalImplantComponent, EntGotRemovedFromContainerMessage>(OnRemove);
}
private void OnInsert(EntityUid uid, SubdermalImplantComponent component, EntGotInsertedIntoContainerMessage args)
{
if (component.ImplantedEntity == null)
return;
if (component.ImplantAction != null)
{
var action = new InstantAction(_prototypeManager.Index<InstantActionPrototype>(component.ImplantAction));
_actionsSystem.AddAction(component.ImplantedEntity.Value, action, uid);
}
//replace micro bomb with macro bomb
if (_container.TryGetContainer(component.ImplantedEntity.Value, ImplanterComponent.ImplantSlotId, out var implantContainer) && _tag.HasTag(uid, "MacroBomb"))
{
foreach (var implant in implantContainer.ContainedEntities)
{
if (_tag.HasTag(implant, "MicroBomb"))
{
implantContainer.Remove(implant);
QueueDel(implant);
}
}
}
}
private void OnRemoveAttempt(EntityUid uid, SubdermalImplantComponent component, ContainerGettingRemovedAttemptEvent args)
{
if (component.Permanent && component.ImplantedEntity != null)
args.Cancel();
}
private void OnRemove(EntityUid uid, SubdermalImplantComponent component, EntGotRemovedFromContainerMessage args)
{
if (component.ImplantedEntity == null)
return;
var entCoords = Transform(component.ImplantedEntity.Value).Coordinates;
if (component.ImplantAction != null)
_actionsSystem.RemoveProvidedActions(component.ImplantedEntity.Value, uid);
if (!_container.TryGetContainer(uid, BaseStorageId, out var storageImplant))
return;
_container.EmptyContainer(storageImplant, moveTo: entCoords);
}
/// <summary>
/// Forces an implant into a person
/// Good for on spawn related code or admin additions
/// </summary>
/// <param name="target">The entity to be implanted</param>
/// <param name="implant"> The implant</param>
/// <param name="component">The implant component</param>
public void ForceImplant(EntityUid target, EntityUid implant, SubdermalImplantComponent component)
{
//If the target doesn't have the implanted component, add it.
var implantedComp = EnsureComp<ImplantedComponent>(target);
var implantContainer = implantedComp.ImplantContainer;
component.ImplantedEntity = target;
implantContainer.Insert(implant);
}
/// <summary>
/// Force remove a singular implant
/// </summary>
/// <param name="target">the implanted entity</param>
/// <param name="implant">the implant</param>
/// <param name="component">the implant component</param>
public void ForceRemove(EntityUid target, EntityUid implant)
{
if (!TryComp<ImplantedComponent>(target, out var implanted))
return;
var implantContainer = implanted.ImplantContainer;
implantContainer.Remove(implant);
QueueDel(implant);
}
/// <summary>
/// Removes and deletes implants by force
/// </summary>
/// <param name="target">The entity to have implants removed</param>
public void WipeImplants(EntityUid target)
{
if (!TryComp<ImplantedComponent>(target, out var implanted))
return;
var implantContainer = implanted.ImplantContainer;
_container.CleanContainer(implantContainer);
}
}