Files
tbd-station-14/Content.Shared/Implants/SharedImplanterSystem.cs
Zachary Higgs c5045883ac Fake mindshield componentry and Implanter (#34079)
* Fake Mindshield (With some bad sprites)

- Add FakeMindshield System and Component

- Add FakeMindsheildImplantSystem and Component

- modify ShowMindShieldIconsSystem to check for FakeMindshields

- add all supporting yaml for the Implants, action and uplink

- add loc file stuff

- add unfinished sprites

* Cleanup, add to thief toolbox, remove metagame

- Move Implant sameness check to AFTER the implant DoAfter
to prevent instant identification of Deception Implants

- cleanup the systems and components

- add the fake mindshield to the Thief toolbox

* part 1 of fixing the folder problem

* Make the fakemindshield sprite folder lowercase

* CR - Move ImplantCheck into shared, cleanup

- Moved ImplantCheck and eventsubscription into Shared

- Remove Client/Server extensions of FakeMindshieldImplantSystem and
FakeMindShieldSystem and make shared Sealed

- make OnToggleMindshield Private, use the event!

* CR - Cleanup extra lines, fix some Prototype

- cleaned up extra liens in ImplanterSystem and
SharedFakeMindshieldSystem from when i was developing

- Uplink catalog no longer lists the implant in 2 spots,
only implants now, also uses the On state action icon

- added a comment about why it's reraising the action event
rather than directly interacting with the FakeMindshield Component

* Fake Mindshield CR:

- Added a comment about IsEnabled

- moved OnFakeMindShieldToggle to Entity<> from Uid, Comp

- fixed some formatting in uplink_catalog

* CR - Add a bit more comment
2025-01-27 18:37:46 -08:00

246 lines
9.3 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.DoAfter;
using Content.Shared.Examine;
using Content.Shared.Forensics;
using Content.Shared.IdentityManagement;
using Content.Shared.Implants.Components;
using Content.Shared.Popups;
using Content.Shared.Whitelist;
using Robust.Shared.Containers;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
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!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ImplanterComponent, ComponentInit>(OnImplanterInit);
SubscribeLocalEvent<ImplanterComponent, EntInsertedIntoContainerMessage>(OnEntInserted);
SubscribeLocalEvent<ImplanterComponent, ExaminedEvent>(OnExamine);
}
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);
}
private void OnExamine(EntityUid uid, ImplanterComponent component, ExaminedEvent args)
{
if (!component.ImplanterSlot.HasItem || !args.IsInDetailsRange)
return;
args.PushMarkup(Loc.GetString("implanter-contained-implant-text", ("desc", component.ImplantData.Item2)));
}
public bool CheckSameImplant(EntityUid target, EntityUid implant)
{
if (!TryComp<ImplantedComponent>(target, out var implanted))
return false;
var implantPrototype = Prototype(implant);
return implanted.ImplantContainer.ContainedEntities.Any(entity => Prototype(entity) == implantPrototype);
}
//Instantly implant something and add all necessary components and containers.
//Set to draw mode if not implant only
public void Implant(EntityUid user, EntityUid target, EntityUid implanter, ImplanterComponent component)
{
if (!CanImplant(user, target, implanter, component, out var implant, out var implantComp))
return;
// Check if we are trying to implant a implant which is already implanted
// Check AFTER the doafter to prevent "is it a fake?" metagaming against deceptive implants
if (!component.AllowMultipleImplants && CheckSameImplant(target, implant.Value))
{
var name = Identity.Name(target, EntityManager, user);
var msg = Loc.GetString("implanter-component-implant-already", ("implant", implant), ("target", name));
_popup.PopupEntity(msg, target, user);
return;
}
//If the target doesn't have the implanted component, add it.
var implantedComp = EnsureComp<ImplantedComponent>(target);
var implantContainer = implantedComp.ImplantContainer;
if (component.ImplanterSlot.ContainerSlot != null)
_container.Remove(implant.Value, component.ImplanterSlot.ContainerSlot);
implantComp.ImplantedEntity = target;
implantContainer.OccludesLight = false;
_container.Insert(implant.Value, implantContainer);
if (component.CurrentMode == ImplanterToggleMode.Inject && !component.ImplantOnly)
DrawMode(implanter, component);
else
ImplantMode(implanter, component);
var ev = new TransferDnaEvent { Donor = target, Recipient = implanter };
RaiseLocalEvent(target, ref ev);
Dirty(implanter, component);
}
public bool CanImplant(
EntityUid user,
EntityUid target,
EntityUid implanter,
ImplanterComponent component,
[NotNullWhen(true)] out EntityUid? implant,
[NotNullWhen(true)] out SubdermalImplantComponent? implantComp)
{
implant = component.ImplanterSlot.ContainerSlot?.ContainedEntities.FirstOrNull();
if (!TryComp(implant, out implantComp))
return false;
if (!CheckTarget(target, component.Whitelist, component.Blacklist) ||
!CheckTarget(target, implantComp.Whitelist, implantComp.Blacklist))
{
return false;
}
var ev = new AddImplantAttemptEvent(user, target, implant.Value, implanter);
RaiseLocalEvent(target, ev);
return !ev.Cancelled;
}
protected bool CheckTarget(EntityUid target, EntityWhitelist? whitelist, EntityWhitelist? blacklist)
{
return _whitelistSystem.IsWhitelistPassOrNull(whitelist, target) &&
_whitelistSystem.IsBlacklistFailOrNull(blacklist, target);
}
//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))
continue;
//Don't remove a permanent implant and look for the next that can be drawn
if (!_container.CanRemove(implant, implantContainer))
{
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, user);
permanentFound = implantComp.Permanent;
continue;
}
_container.Remove(implant, implantContainer);
implantComp.ImplantedEntity = null;
_container.Insert(implant, implanterContainer);
permanentFound = implantComp.Permanent;
var ev = new TransferDnaEvent { Donor = target, Recipient = implanter };
RaiseLocalEvent(target, ref ev);
//Break so only one implant is drawn
break;
}
if (component.CurrentMode == ImplanterToggleMode.Draw && !component.ImplantOnly && !permanentFound)
ImplantMode(implanter, component);
Dirty(implanter, component);
}
}
private void ImplantMode(EntityUid uid, ImplanterComponent component)
{
component.CurrentMode = ImplanterToggleMode.Inject;
ChangeOnImplantVisualizer(uid, component);
}
private void DrawMode(EntityUid uid, ImplanterComponent component)
{
component.CurrentMode = ImplanterToggleMode.Draw;
ChangeOnImplantVisualizer(uid, component);
}
private void ChangeOnImplantVisualizer(EntityUid uid, ImplanterComponent component)
{
if (!TryComp<AppearanceComponent>(uid, out var appearance))
return;
bool implantFound;
if (component.ImplanterSlot.HasItem)
implantFound = true;
else
implantFound = false;
if (component.CurrentMode == ImplanterToggleMode.Inject && !component.ImplantOnly)
_appearance.SetData(uid, ImplanterVisuals.Full, implantFound, appearance);
else if (component.CurrentMode == ImplanterToggleMode.Inject && component.ImplantOnly)
{
_appearance.SetData(uid, ImplanterVisuals.Full, implantFound, appearance);
_appearance.SetData(uid, ImplanterImplantOnlyVisuals.ImplantOnly, component.ImplantOnly,
appearance);
}
else
_appearance.SetData(uid, ImplanterVisuals.Full, implantFound, appearance);
}
}
[Serializable, NetSerializable]
public sealed partial class ImplantEvent : SimpleDoAfterEvent
{
}
[Serializable, NetSerializable]
public sealed partial class DrawEvent : SimpleDoAfterEvent
{
}
public sealed class AddImplantAttemptEvent : CancellableEntityEventArgs
{
public readonly EntityUid User;
public readonly EntityUid Target;
public readonly EntityUid Implant;
public readonly EntityUid Implanter;
public AddImplantAttemptEvent(EntityUid user, EntityUid target, EntityUid implant, EntityUid implanter)
{
User = user;
Target = target;
Implant = implant;
Implanter = implanter;
}
}