ninja 2 electric boogaloo (#15534)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-09-10 07:20:27 +01:00
committed by GitHub
parent 25c8a03276
commit 24810d916b
153 changed files with 3892 additions and 78 deletions

View File

@@ -0,0 +1,100 @@
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Ninja.Components;
using Content.Shared.Ninja.Systems;
using Content.Shared.Popups;
using Robust.Shared.Audio;
namespace Content.Server.Ninja.Systems;
/// <summary>
/// Handles the doafter and power transfer when draining.
/// </summary>
public sealed class BatteryDrainerSystem : SharedBatteryDrainerSystem
{
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BatteryDrainerComponent, BeforeInteractHandEvent>(OnBeforeInteractHand);
}
/// <summary>
/// Start do after for draining a power source.
/// Can't predict PNBC existing so only done on server.
/// </summary>
private void OnBeforeInteractHand(EntityUid uid, BatteryDrainerComponent comp, BeforeInteractHandEvent args)
{
var target = args.Target;
if (args.Handled || comp.BatteryUid == null || !HasComp<PowerNetworkBatteryComponent>(target))
return;
// handles even if battery is full so you can actually see the poup
args.Handled = true;
if (_battery.IsFull(comp.BatteryUid.Value))
{
_popup.PopupEntity(Loc.GetString("battery-drainer-full"), uid, uid, PopupType.Medium);
return;
}
var doAfterArgs = new DoAfterArgs(uid, comp.DrainTime, new DrainDoAfterEvent(), target: target, eventTarget: uid)
{
BreakOnUserMove = true,
MovementThreshold = 0.5f,
CancelDuplicate = false,
AttemptFrequency = AttemptFrequency.StartAndEnd
};
_doAfter.TryStartDoAfter(doAfterArgs);
}
/// <inheritdoc/>
protected override void OnDoAfterAttempt(EntityUid uid, BatteryDrainerComponent comp, DoAfterAttemptEvent<DrainDoAfterEvent> args)
{
base.OnDoAfterAttempt(uid, comp, args);
if (comp.BatteryUid == null || _battery.IsFull(comp.BatteryUid.Value))
args.Cancel();
}
/// <inheritdoc/>
protected override bool TryDrainPower(EntityUid uid, BatteryDrainerComponent comp, EntityUid target)
{
if (comp.BatteryUid == null || !TryComp<BatteryComponent>(comp.BatteryUid.Value, out var battery))
return false;
if (!TryComp<BatteryComponent>(target, out var targetBattery) || !TryComp<PowerNetworkBatteryComponent>(target, out var pnb))
return false;
if (MathHelper.CloseToPercent(targetBattery.CurrentCharge, 0))
{
_popup.PopupEntity(Loc.GetString("battery-drainer-empty", ("battery", target)), uid, uid, PopupType.Medium);
return false;
}
var available = targetBattery.CurrentCharge;
var required = battery.MaxCharge - battery.CurrentCharge;
// higher tier storages can charge more
var maxDrained = pnb.MaxSupply * comp.DrainTime;
var input = Math.Min(Math.Min(available, required / comp.DrainEfficiency), maxDrained);
if (!_battery.TryUseCharge(target, input, targetBattery))
return false;
var output = input * comp.DrainEfficiency;
_battery.SetCharge(comp.BatteryUid.Value, battery.CurrentCharge + output, battery);
Spawn("EffectSparks", Transform(target).Coordinates);
_audio.PlayPvs(comp.SparkSound, target);
_popup.PopupEntity(Loc.GetString("battery-drainer-success", ("battery", target)), uid, uid);
// repeat the doafter until battery is full
return !battery.IsFullyCharged;
}
}

View File

@@ -0,0 +1,102 @@
using Content.Server.Communications;
using Content.Server.DoAfter;
using Content.Server.Mind;
using Content.Server.Ninja.Systems;
using Content.Server.Power.Components;
using Content.Server.Roles;
using Content.Shared.Communications;
using Content.Shared.DoAfter;
using Content.Shared.Interaction.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Ninja.Components;
using Content.Shared.Ninja.Systems;
using Content.Shared.Popups;
using Content.Shared.Research.Components;
using Content.Shared.Toggleable;
namespace Content.Server.Ninja.Systems;
/// <summary>
/// Handles the toggle gloves action.
/// </summary>
public sealed class NinjaGlovesSystem : SharedNinjaGlovesSystem
{
[Dependency] private readonly EmagProviderSystem _emagProvider = default!;
[Dependency] private readonly SharedBatteryDrainerSystem _drainer = default!;
[Dependency] private readonly SharedStunProviderSystem _stunProvider = default!;
[Dependency] private readonly SpaceNinjaSystem _ninja = default!;
[Dependency] private readonly CommsHackerSystem _commsHacker = default!;
[Dependency] private readonly MindSystem _mind = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NinjaGlovesComponent, ToggleActionEvent>(OnToggleAction);
}
/// <summary>
/// Toggle gloves, if the user is a ninja wearing a ninja suit.
/// </summary>
private void OnToggleAction(EntityUid uid, NinjaGlovesComponent comp, ToggleActionEvent args)
{
if (args.Handled)
return;
args.Handled = true;
var user = args.Performer;
// need to wear suit to enable gloves
if (!TryComp<SpaceNinjaComponent>(user, out var ninja)
|| ninja.Suit == null
|| !HasComp<NinjaSuitComponent>(ninja.Suit.Value))
{
Popup.PopupEntity(Loc.GetString("ninja-gloves-not-wearing-suit"), user, user);
return;
}
// show its state to the user
var enabling = comp.User == null;
Appearance.SetData(uid, ToggleVisuals.Toggled, enabling);
var message = Loc.GetString(enabling ? "ninja-gloves-on" : "ninja-gloves-off");
Popup.PopupEntity(message, user, user);
if (enabling)
{
EnableGloves(uid, comp, user, ninja);
}
else
{
DisableGloves(uid, comp);
}
}
private void EnableGloves(EntityUid uid, NinjaGlovesComponent comp, EntityUid user, SpaceNinjaComponent ninja)
{
comp.User = user;
Dirty(uid, comp);
_ninja.AssignGloves(user, uid, ninja);
var drainer = EnsureComp<BatteryDrainerComponent>(user);
var stun = EnsureComp<StunProviderComponent>(user);
_stunProvider.SetNoPowerPopup(user, "ninja-no-power", stun);
if (_ninja.GetNinjaBattery(user, out var battery, out var _))
{
_drainer.SetBattery(user, battery, drainer);
_stunProvider.SetBattery(user, battery, stun);
}
var emag = EnsureComp<EmagProviderComponent>(user);
_emagProvider.SetWhitelist(user, comp.DoorjackWhitelist, emag);
EnsureComp<ResearchStealerComponent>(user);
// prevent calling in multiple threats by toggling gloves after
if (_mind.TryGetRole<NinjaRoleComponent>(user, out var role) && !role.CalledInThreat)
{
var hacker = EnsureComp<CommsHackerComponent>(user);
var rule = _ninja.NinjaRule(user);
if (rule != null)
_commsHacker.SetThreats(user, rule.Threats, hacker);
}
}
}

View File

@@ -0,0 +1,147 @@
using Content.Server.Emp;
using Content.Server.Popups;
using Content.Server.Power.Components;
using Content.Server.PowerCell;
using Content.Shared.Actions;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Ninja.Components;
using Content.Shared.Ninja.Systems;
using Content.Shared.Popups;
using Robust.Shared.Containers;
namespace Content.Server.Ninja.Systems;
/// <summary>
/// Handles power cell upgrading and actions.
/// </summary>
public sealed class NinjaSuitSystem : SharedNinjaSuitSystem
{
[Dependency] private readonly EmpSystem _emp = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SpaceNinjaSystem _ninja = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NinjaSuitComponent, ContainerIsInsertingAttemptEvent>(OnSuitInsertAttempt);
SubscribeLocalEvent<NinjaSuitComponent, EmpAttemptEvent>(OnEmpAttempt);
SubscribeLocalEvent<NinjaSuitComponent, AttemptStealthEvent>(OnAttemptStealth);
SubscribeLocalEvent<NinjaSuitComponent, CreateThrowingStarEvent>(OnCreateThrowingStar);
SubscribeLocalEvent<NinjaSuitComponent, RecallKatanaEvent>(OnRecallKatana);
SubscribeLocalEvent<NinjaSuitComponent, NinjaEmpEvent>(OnEmp);
}
protected override void NinjaEquippedSuit(EntityUid uid, NinjaSuitComponent comp, EntityUid user, SpaceNinjaComponent ninja)
{
base.NinjaEquippedSuit(uid, comp, user, ninja);
_ninja.SetSuitPowerAlert(user);
}
// TODO: if/when battery is in shared, put this there too
// TODO: or put MaxCharge in shared along with powercellslot
private void OnSuitInsertAttempt(EntityUid uid, NinjaSuitComponent comp, ContainerIsInsertingAttemptEvent args)
{
// no power cell for some reason??? allow it
if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery))
return;
// can only upgrade power cell, not swap to recharge instantly otherwise ninja could just swap batteries with flashlights in maints for easy power
if (!TryComp<BatteryComponent>(args.EntityUid, out var inserting) || inserting.MaxCharge <= battery.MaxCharge)
{
args.Cancel();
}
// TODO: raise event on ninja telling it to update battery
}
private void OnEmpAttempt(EntityUid uid, NinjaSuitComponent comp, EmpAttemptEvent args)
{
// ninja suit (battery) is immune to emp
// powercell relays the event to suit
args.Cancel();
}
protected override void UserUnequippedSuit(EntityUid uid, NinjaSuitComponent comp, EntityUid user)
{
base.UserUnequippedSuit(uid, comp, user);
// remove power indicator
_ninja.SetSuitPowerAlert(user);
}
private void OnAttemptStealth(EntityUid uid, NinjaSuitComponent comp, AttemptStealthEvent args)
{
var user = args.User;
// need 1 second of charge to turn on stealth
var chargeNeeded = SuitWattage(uid, comp);
// being attacked while cloaked gives no power message since it overloads the power supply or something
if (!_ninja.GetNinjaBattery(user, out var _, out var battery) || battery.CurrentCharge < chargeNeeded || UseDelay.ActiveDelay(user))
{
_popup.PopupEntity(Loc.GetString("ninja-no-power"), user, user);
args.Cancel();
return;
}
StealthClothing.SetEnabled(uid, user, true);
}
private void OnCreateThrowingStar(EntityUid uid, NinjaSuitComponent comp, CreateThrowingStarEvent args)
{
args.Handled = true;
var user = args.Performer;
if (!_ninja.TryUseCharge(user, comp.ThrowingStarCharge) || UseDelay.ActiveDelay(user))
{
_popup.PopupEntity(Loc.GetString("ninja-no-power"), user, user);
return;
}
// try to put throwing star in hand, otherwise it goes on the ground
var star = Spawn(comp.ThrowingStarPrototype, Transform(user).Coordinates);
_hands.TryPickupAnyHand(user, star);
}
private void OnRecallKatana(EntityUid uid, NinjaSuitComponent comp, RecallKatanaEvent args)
{
args.Handled = true;
var user = args.Performer;
if (!TryComp<SpaceNinjaComponent>(user, out var ninja) || ninja.Katana == null)
return;
var katana = ninja.Katana.Value;
var coords = _transform.GetWorldPosition(katana);
var distance = (_transform.GetWorldPosition(user) - coords).Length();
var chargeNeeded = (float) distance * comp.RecallCharge;
if (!_ninja.TryUseCharge(user, chargeNeeded) || UseDelay.ActiveDelay(user))
{
_popup.PopupEntity(Loc.GetString("ninja-no-power"), user, user);
return;
}
// TODO: teleporting into belt slot
var message = _hands.TryPickupAnyHand(user, katana)
? "ninja-katana-recalled"
: "ninja-hands-full";
_popup.PopupEntity(Loc.GetString(message), user, user);
}
private void OnEmp(EntityUid uid, NinjaSuitComponent comp, NinjaEmpEvent args)
{
args.Handled = true;
var user = args.Performer;
if (!_ninja.TryUseCharge(user, comp.EmpCharge) || UseDelay.ActiveDelay(user))
{
_popup.PopupEntity(Loc.GetString("ninja-no-power"), user, user);
return;
}
// I don't think this affects the suit battery, but if it ever does in the future add a blacklist for it
var coords = Transform(user).MapPosition;
_emp.EmpPulse(coords, comp.EmpRange, comp.EmpConsumption, comp.EmpDuration);
}
}

View File

@@ -0,0 +1,301 @@
using Content.Server.Administration.Commands;
using Content.Server.Communications;
using Content.Server.Chat.Managers;
using Content.Server.StationEvents.Components;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Rules;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.Ghost.Roles.Events;
using Content.Server.Objectives;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.PowerCell;
using Content.Server.Research.Systems;
using Content.Server.Roles;
using Content.Server.Warps;
using Content.Shared.Alert;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Doors.Components;
using Content.Shared.IdentityManagement;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Content.Shared.Ninja.Components;
using Content.Shared.Ninja.Systems;
using Content.Shared.Popups;
using Content.Shared.Roles;
using Content.Shared.PowerCell.Components;
using Content.Shared.Rounding;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;
using Robust.Shared.Random;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Content.Server.Ninja.Systems;
// TODO: when syndiborgs are a thing have a borg converter with 6 second doafter
// engi -> saboteur
// medi -> idk reskin it
// other -> assault
// TODO: when criminal records is merged, hack it to set everyone to arrest
/// <summary>
/// Main ninja system that handles ninja setup and greentext, provides helper methods for the rest of the code to use.
/// </summary>
public sealed class SpaceNinjaSystem : SharedSpaceNinjaSystem
{
[Dependency] private readonly AlertsSystem _alerts = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly IChatManager _chatMan = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly RoleSystem _role = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly StealthClothingSystem _stealthClothing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpaceNinjaComponent, MindAddedMessage>(OnNinjaMindAdded);
SubscribeLocalEvent<SpaceNinjaComponent, EmaggedSomethingEvent>(OnDoorjack);
SubscribeLocalEvent<SpaceNinjaComponent, ResearchStolenEvent>(OnResearchStolen);
SubscribeLocalEvent<SpaceNinjaComponent, ThreatCalledInEvent>(OnThreatCalledIn);
}
public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<SpaceNinjaComponent>();
while (query.MoveNext(out var uid, out var ninja))
{
UpdateNinja(uid, ninja, frameTime);
}
}
/// <summary>
/// Turns the player into a space ninja
/// </summary>
public void MakeNinja(EntityUid mindId, MindComponent mind)
{
if (mind.OwnedEntity == null)
return;
// prevent double ninja'ing
var user = mind.OwnedEntity.Value;
if (HasComp<SpaceNinjaComponent>(user))
return;
AddComp<SpaceNinjaComponent>(user);
SetOutfitCommand.SetOutfit(user, "SpaceNinjaGear", EntityManager);
GreetNinja(mindId, mind);
}
/// <summary>
/// Download the given set of nodes, returning how many new nodes were downloaded.
/// </summary>
private int Download(EntityUid uid, List<string> ids)
{
if (!_mind.TryGetRole<NinjaRoleComponent>(uid, out var role))
return 0;
var oldCount = role.DownloadedNodes.Count;
role.DownloadedNodes.UnionWith(ids);
var newCount = role.DownloadedNodes.Count;
return newCount - oldCount;
}
/// <summary>
/// Returns a ninja's gamerule config data.
/// If the gamerule was not started then it will be started automatically.
/// </summary>
public NinjaRuleComponent? NinjaRule(EntityUid uid, SpaceNinjaComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return null;
// already exists so just check it
if (comp.Rule != null)
return CompOrNull<NinjaRuleComponent>(comp.Rule);
// start it
_gameTicker.StartGameRule("Ninja", out var rule);
comp.Rule = rule;
if (!TryComp<NinjaRuleComponent>(rule, out var ninjaRule))
return null;
// add ninja mind to the rule's list for objective showing
if (TryComp<MindContainerComponent>(uid, out var mindContainer) && mindContainer.Mind != null)
{
ninjaRule.Minds.Add(mindContainer.Mind.Value);
}
return ninjaRule;
}
// TODO: can probably copy paste borg code here
/// <summary>
/// Update the alert for the ninja's suit power indicator.
/// </summary>
public void SetSuitPowerAlert(EntityUid uid, SpaceNinjaComponent? comp = null)
{
if (!Resolve(uid, ref comp, false) || comp.Deleted || comp.Suit == null)
{
_alerts.ClearAlert(uid, AlertType.SuitPower);
return;
}
if (GetNinjaBattery(uid, out var _, out var battery))
{
var severity = ContentHelpers.RoundToLevels(MathF.Max(0f, battery.CurrentCharge), battery.MaxCharge, 8);
_alerts.ShowAlert(uid, AlertType.SuitPower, (short) severity);
}
else
{
_alerts.ClearAlert(uid, AlertType.SuitPower);
}
}
/// <summary>
/// Get the battery component in a ninja's suit, if it's worn.
/// </summary>
public bool GetNinjaBattery(EntityUid user, [NotNullWhen(true)] out EntityUid? uid, [NotNullWhen(true)] out BatteryComponent? battery)
{
if (TryComp<SpaceNinjaComponent>(user, out var ninja)
&& ninja.Suit != null
&& _powerCell.TryGetBatteryFromSlot(ninja.Suit.Value, out uid, out battery))
{
return true;
}
uid = null;
battery = null;
return false;
}
/// <inheritdoc/>
public override bool TryUseCharge(EntityUid user, float charge)
{
return GetNinjaBattery(user, out var uid, out var battery) && _battery.TryUseCharge(uid.Value, charge, battery);
}
/// <summary>
/// Greets the ninja when a ghost takes over a ninja, if that happens.
/// </summary>
private void OnNinjaMindAdded(EntityUid uid, SpaceNinjaComponent comp, MindAddedMessage args)
{
if (TryComp<MindContainerComponent>(uid, out var mind) && mind.Mind != null)
GreetNinja(mind.Mind.Value);
}
/// <summary>
/// Set up everything for ninja to work and send the greeting message/sound.
/// </summary>
private void GreetNinja(EntityUid mindId, MindComponent? mind = null)
{
if (!Resolve(mindId, ref mind) || mind.OwnedEntity == null || mind.Session == null)
return;
var uid = mind.OwnedEntity.Value;
var config = NinjaRule(uid);
if (config == null)
return;
var role = new NinjaRoleComponent
{
PrototypeId = "SpaceNinja"
};
_role.MindAddRole(mindId, role, mind);
// choose spider charge detonation point
// currently based on warp points, something better could be done (but would likely require mapping work)
var warps = new List<EntityUid>();
var query = EntityQueryEnumerator<WarpPointComponent, TransformComponent>();
var map = Transform(uid).MapID;
while (query.MoveNext(out var warpUid, out var warp, out var xform))
{
// won't be asked to detonate the nuke disk or singularity or centcomm
if (warp.Location != null && !HasComp<PhysicsComponent>(warpUid) && xform.MapID == map)
warps.Add(warpUid);
}
if (warps.Count > 0)
role.SpiderChargeTarget = _random.Pick(warps);
// assign objectives - must happen after spider charge target so that the obj requirement works
foreach (var objective in config.Objectives)
{
if (!_mind.TryAddObjective(mindId, objective, mind))
{
Log.Error($"Failed to add {objective} to ninja {mind.OwnedEntity.Value}");
}
}
var session = mind.Session;
_audio.PlayGlobal(config.GreetingSound, Filter.Empty().AddPlayer(session), false, AudioParams.Default);
_chatMan.DispatchServerMessage(session, Loc.GetString("ninja-role-greeting"));
}
// TODO: PowerCellDraw, modify when cloak enabled
/// <summary>
/// Handle constant power drains from passive usage and cloak.
/// </summary>
private void UpdateNinja(EntityUid uid, SpaceNinjaComponent ninja, float frameTime)
{
if (ninja.Suit == null)
return;
float wattage = _suit.SuitWattage(ninja.Suit.Value);
SetSuitPowerAlert(uid, ninja);
if (!TryUseCharge(uid, wattage * frameTime))
{
// ran out of power, uncloak ninja
_stealthClothing.SetEnabled(ninja.Suit.Value, uid, false);
}
}
/// <summary>
/// Increment greentext when emagging a door.
/// </summary>
private void OnDoorjack(EntityUid uid, SpaceNinjaComponent comp, ref EmaggedSomethingEvent args)
{
// incase someone lets ninja emag non-doors double check it here
if (!HasComp<DoorComponent>(args.Target))
return;
// this popup is serverside since door emag logic is serverside (power funnies)
_popup.PopupEntity(Loc.GetString("ninja-doorjack-success", ("target", Identity.Entity(args.Target, EntityManager))), uid, uid, PopupType.Medium);
// handle greentext
if (_mind.TryGetRole<NinjaRoleComponent>(uid, out var role))
role.DoorsJacked++;
}
/// <summary>
/// Add to greentext when stealing technologies.
/// </summary>
private void OnResearchStolen(EntityUid uid, SpaceNinjaComponent comp, ref ResearchStolenEvent args)
{
var gained = Download(uid, args.Techs);
var str = gained == 0
? Loc.GetString("ninja-research-steal-fail")
: Loc.GetString("ninja-research-steal-success", ("count", gained), ("server", args.Target));
_popup.PopupEntity(str, uid, uid, PopupType.Medium);
}
private void OnThreatCalledIn(EntityUid uid, SpaceNinjaComponent comp, ref ThreatCalledInEvent args)
{
if (_mind.TryGetRole<NinjaRoleComponent>(uid, out var role))
{
role.CalledInThreat = true;
}
}
}

View File

@@ -0,0 +1,78 @@
using Content.Server.Explosion.EntitySystems;
using Content.Server.Mind;
using Content.Server.Popups;
using Content.Server.Roles;
using Content.Server.Sticky.Events;
using Content.Shared.Interaction;
using Content.Shared.Ninja.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Ninja.Systems;
/// <summary>
/// Prevents planting a spider charge outside of its location and handles greentext.
/// </summary>
public sealed class SpiderChargeSystem : EntitySystem
{
[Dependency] private readonly MindSystem _mind = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpiderChargeComponent, BeforeRangedInteractEvent>(BeforePlant);
SubscribeLocalEvent<SpiderChargeComponent, EntityStuckEvent>(OnStuck);
SubscribeLocalEvent<SpiderChargeComponent, TriggerEvent>(OnExplode);
}
/// <summary>
/// Require that the planter is a ninja and the charge is near the target warp point.
/// </summary>
private void BeforePlant(EntityUid uid, SpiderChargeComponent comp, BeforeRangedInteractEvent args)
{
var user = args.User;
if (!_mind.TryGetRole<NinjaRoleComponent>(user, out var role))
{
_popup.PopupEntity(Loc.GetString("spider-charge-not-ninja"), user, user);
args.Handled = true;
return;
}
// allow planting anywhere if there is no target, which should never happen
if (role.SpiderChargeTarget == null)
return;
// assumes warp point still exists
var target = Transform(role.SpiderChargeTarget.Value).MapPosition;
var coords = args.ClickLocation.ToMap(EntityManager, _transform);
if (!coords.InRange(target, comp.Range))
{
_popup.PopupEntity(Loc.GetString("spider-charge-too-far"), user, user);
args.Handled = true;
}
}
/// <summary>
/// Allows greentext to occur after exploding.
/// </summary>
private void OnStuck(EntityUid uid, SpiderChargeComponent comp, EntityStuckEvent args)
{
comp.Planter = args.User;
}
/// <summary>
/// Handles greentext after exploding.
/// Assumes it didn't move and the target was destroyed so be nice.
/// </summary>
private void OnExplode(EntityUid uid, SpiderChargeComponent comp, TriggerEvent args)
{
if (comp.Planter == null || !_mind.TryGetRole<NinjaRoleComponent>(comp.Planter.Value, out var role))
return;
// assumes the target was destroyed, that the charge wasn't moved somehow
role.SpiderChargeDetonated = true;
}
}

View File

@@ -0,0 +1,60 @@
using Content.Shared.Electrocution;
using Content.Shared.Interaction;
using Content.Shared.Ninja.Components;
using Content.Shared.Ninja.Systems;
using Content.Shared.Popups;
using Content.Shared.Whitelist;
using Content.Server.Power.EntitySystems;
using Robust.Shared.Timing;
namespace Content.Server.Ninja.Systems;
/// <summary>
/// Shocks clicked mobs using battery charge.
/// </summary>
public sealed class StunProviderSystem : SharedStunProviderSystem
{
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedElectrocutionSystem _electrocution = default!;
[Dependency] private readonly SharedNinjaGlovesSystem _gloves = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StunProviderComponent, BeforeInteractHandEvent>(OnBeforeInteractHand);
}
/// <summary>
/// Stun clicked mobs on the whitelist, if there is enough power.
/// </summary>
private void OnBeforeInteractHand(EntityUid uid, StunProviderComponent comp, BeforeInteractHandEvent args)
{
// TODO: generic check
if (args.Handled || comp.BatteryUid == null || !_gloves.AbilityCheck(uid, args, out var target))
return;
if (target == uid || !comp.Whitelist.IsValid(target, EntityManager))
return;
if (_timing.CurTime < comp.NextStun)
return;
// take charge from battery
if (!_battery.TryUseCharge(comp.BatteryUid.Value, comp.StunCharge))
{
_popup.PopupEntity(Loc.GetString(comp.NoPowerPopup), uid, uid);
return;
}
// not holding hands with target so insuls don't matter
_electrocution.TryDoElectrocution(target, uid, comp.StunDamage, comp.StunTime, false, ignoreInsulation: true);
// short cooldown to prevent instant stunlocking
comp.NextStun = _timing.CurTime + comp.Cooldown;
Dirty(uid, comp);
args.Handled = true;
}
}