mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-17 05:27:38 +03:00
<!-- This is a semi-strict format, you can add/remove sections as needed but the order/format should be kept the same Remove these comments before submitting --> # Description the adding AI is now up to y'all because i'm not touching loadout code for name datasets, but it shouldn't be too bad from here --------- Signed-off-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com> Signed-off-by: SolStar <44028047+ewokswagger@users.noreply.github.com> Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: themias <89101928+themias@users.noreply.github.com> Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com> Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com> Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com> Co-authored-by: ravage <142820619+ravage123321@users.noreply.github.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: Intoxicating-Innocence <188202277+Intoxicating-Innocence@users.noreply.github.com> Co-authored-by: Saphire <lattice@saphi.re> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: CaasGit <87243814+CaasGit@users.noreply.github.com> Co-authored-by: BramvanZijp <56019239+BramvanZijp@users.noreply.github.com> Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com> Co-authored-by: NakataRin <45946146+NakataRin@users.noreply.github.com> Co-authored-by: Kara <lunarautomaton6@gmail.com> Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com> Co-authored-by: SlamBamActionman <slambamactionman@gmail.com> Co-authored-by: Doomsdrayk <robotdoughnut@comcast.net> Co-authored-by: Brandon Hu <103440971+Brandon-Huu@users.noreply.github.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: ElectroJr <leonsfriedrich@gmail.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: Julian Giebel <juliangiebel@live.de> Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com> Co-authored-by: Chief-Engineer <119664036+Chief-Engineer@users.noreply.github.com> Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com> Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Co-authored-by: no <165581243+pissdemon@users.noreply.github.com> Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com> Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com> Co-authored-by: TGRCDev <tgrc@tgrc.dev> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com> Co-authored-by: Fildrance <fildrance@gmail.com> Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru> Co-authored-by: chavonadelal <156101927+chavonadelal@users.noreply.github.com> Co-authored-by: SolStar <44028047+ewokswagger@users.noreply.github.com> Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: ArchRBX <5040911+ArchRBX@users.noreply.github.com> Co-authored-by: archrbx <punk.gear5260@fastmail.com> Co-authored-by: Radezolid <snappednexus@gmail.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: EmoGarbage404 <retron404@gmail.com> Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com> Co-authored-by: Velcroboy <107660393+IamVelcroboy@users.noreply.github.com> Co-authored-by: Velcroboy <velcroboy333@hotmail.com> Co-authored-by: neuPanda <chriseparton@gmail.com> Co-authored-by: neuPanda <spainman0@yahoo.com> Co-authored-by: Dvir <39403717+dvir001@users.noreply.github.com> Co-authored-by: Whatstone <whatston3@gmail.com> Co-authored-by: VideoKompany <135313844+VlaDOS1408@users.noreply.github.com> (cherry picked from commit 93ed70acfeda357133a701f637d3faeec02749bb)
352 lines
13 KiB
C#
352 lines
13 KiB
C#
using System.Numerics;
|
|
using System.Threading;
|
|
using Content.Server.Administration.Logs;
|
|
using Content.Server.Construction;
|
|
using Content.Server.DeviceLinking.Events;
|
|
using Content.Server.Power.Components;
|
|
using Content.Server.Power.EntitySystems;
|
|
using Content.Server.Projectiles;
|
|
using Content.Server.Weapons.Ranged.Systems;
|
|
using Content.Shared.Database;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Lock;
|
|
using Content.Shared.Popups;
|
|
using Content.Shared.Power;
|
|
using Content.Shared.Projectiles;
|
|
using Content.Shared.Singularity.Components;
|
|
using Content.Shared.Singularity.EntitySystems;
|
|
using Content.Shared.Verbs;
|
|
using Content.Shared.Weapons.Ranged.Components;
|
|
using JetBrains.Annotations;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Physics;
|
|
using Robust.Shared.Physics.Components;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Random;
|
|
using Robust.Shared.Utility;
|
|
using Timer = Robust.Shared.Timing.Timer;
|
|
|
|
namespace Content.Server.Singularity.EntitySystems
|
|
{
|
|
[UsedImplicitly]
|
|
public sealed class EmitterSystem : SharedEmitterSystem
|
|
{
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
|
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
|
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
|
[Dependency] private readonly ProjectileSystem _projectile = default!;
|
|
[Dependency] private readonly GunSystem _gun = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<EmitterComponent, PowerConsumerReceivedChanged>(ReceivedChanged);
|
|
SubscribeLocalEvent<EmitterComponent, PowerChangedEvent>(OnApcChanged);
|
|
SubscribeLocalEvent<EmitterComponent, InteractHandEvent>(OnInteractHand);
|
|
SubscribeLocalEvent<EmitterComponent, GetVerbsEvent<Verb>>(OnGetVerb);
|
|
SubscribeLocalEvent<EmitterComponent, ExaminedEvent>(OnExamined);
|
|
SubscribeLocalEvent<EmitterComponent, RefreshPartsEvent>(OnRefreshParts);
|
|
SubscribeLocalEvent<EmitterComponent, UpgradeExamineEvent>(OnUpgradeExamine);
|
|
SubscribeLocalEvent<EmitterComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
|
|
SubscribeLocalEvent<EmitterComponent, SignalReceivedEvent>(OnSignalReceived);
|
|
}
|
|
|
|
private void OnAnchorStateChanged(EntityUid uid, EmitterComponent component, ref AnchorStateChangedEvent args)
|
|
{
|
|
if (args.Anchored)
|
|
return;
|
|
|
|
SwitchOff(uid, component);
|
|
}
|
|
|
|
private void OnInteractHand(EntityUid uid, EmitterComponent component, InteractHandEvent args)
|
|
{
|
|
if (args.Handled)
|
|
return;
|
|
|
|
if (TryComp(uid, out LockComponent? lockComp) && lockComp.Locked)
|
|
{
|
|
_popup.PopupEntity(Loc.GetString("comp-emitter-access-locked",
|
|
("target", uid)), uid, args.User);
|
|
return;
|
|
}
|
|
|
|
if (TryComp(uid, out PhysicsComponent? phys) && phys.BodyType == BodyType.Static)
|
|
{
|
|
if (!component.IsOn)
|
|
{
|
|
SwitchOn(uid, component);
|
|
_popup.PopupEntity(Loc.GetString("comp-emitter-turned-on",
|
|
("target", uid)), uid, args.User);
|
|
}
|
|
else
|
|
{
|
|
SwitchOff(uid, component);
|
|
_popup.PopupEntity(Loc.GetString("comp-emitter-turned-off",
|
|
("target", uid)), uid, args.User);
|
|
}
|
|
|
|
_adminLogger.Add(LogType.FieldGeneration,
|
|
component.IsOn ? LogImpact.Medium : LogImpact.High,
|
|
$"{ToPrettyString(args.User):player} toggled {ToPrettyString(uid):emitter}");
|
|
args.Handled = true;
|
|
}
|
|
else
|
|
{
|
|
_popup.PopupEntity(Loc.GetString("comp-emitter-not-anchored",
|
|
("target", uid)), uid, args.User);
|
|
}
|
|
}
|
|
|
|
private void OnGetVerb(EntityUid uid, EmitterComponent component, GetVerbsEvent<Verb> args)
|
|
{
|
|
if (!args.CanAccess || !args.CanInteract || args.Hands == null)
|
|
return;
|
|
|
|
if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked)
|
|
return;
|
|
|
|
if (component.SelectableTypes.Count < 2)
|
|
return;
|
|
|
|
foreach (var type in component.SelectableTypes)
|
|
{
|
|
var proto = _prototype.Index<EntityPrototype>(type);
|
|
|
|
var v = new Verb
|
|
{
|
|
Priority = 1,
|
|
Category = VerbCategory.SelectType,
|
|
Text = proto.Name,
|
|
Disabled = type == component.BoltType,
|
|
Impact = LogImpact.Medium,
|
|
DoContactInteraction = true,
|
|
Act = () =>
|
|
{
|
|
component.BoltType = type;
|
|
_popup.PopupEntity(Loc.GetString("emitter-component-type-set", ("type", proto.Name)), uid);
|
|
}
|
|
};
|
|
args.Verbs.Add(v);
|
|
}
|
|
}
|
|
|
|
private void OnExamined(EntityUid uid, EmitterComponent component, ExaminedEvent args)
|
|
{
|
|
if (component.SelectableTypes.Count < 2)
|
|
return;
|
|
var proto = _prototype.Index<EntityPrototype>(component.BoltType);
|
|
args.PushMarkup(Loc.GetString("emitter-component-current-type", ("type", proto.Name)));
|
|
}
|
|
|
|
private void ReceivedChanged(
|
|
EntityUid uid,
|
|
EmitterComponent component,
|
|
ref PowerConsumerReceivedChanged args)
|
|
{
|
|
if (!component.IsOn)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (args.ReceivedPower < args.DrawRate)
|
|
{
|
|
PowerOff(uid, component);
|
|
}
|
|
else
|
|
{
|
|
PowerOn(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnApcChanged(EntityUid uid, EmitterComponent component, ref PowerChangedEvent args)
|
|
{
|
|
if (!component.IsOn)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!args.Powered)
|
|
{
|
|
PowerOff(uid, component);
|
|
}
|
|
else
|
|
{
|
|
PowerOn(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnRefreshParts(EntityUid uid, EmitterComponent component, RefreshPartsEvent args)
|
|
{
|
|
var fireRateRating = args.PartRatings[component.MachinePartFireRate];
|
|
|
|
component.FireInterval = component.BaseFireInterval * MathF.Pow(component.FireRateMultiplier, fireRateRating - 1);
|
|
component.FireBurstDelayMin = component.BaseFireBurstDelayMin * MathF.Pow(component.FireRateMultiplier, fireRateRating - 1);
|
|
component.FireBurstDelayMax = component.BaseFireBurstDelayMax * MathF.Pow(component.FireRateMultiplier, fireRateRating - 1);
|
|
}
|
|
|
|
private void OnUpgradeExamine(EntityUid uid, EmitterComponent component, UpgradeExamineEvent args)
|
|
{
|
|
args.AddPercentageUpgrade("emitter-component-upgrade-fire-rate", (float) (component.BaseFireInterval.TotalSeconds / component.FireInterval.TotalSeconds));
|
|
}
|
|
|
|
public void SwitchOff(EntityUid uid, EmitterComponent component)
|
|
{
|
|
component.IsOn = false;
|
|
if (TryComp<PowerConsumerComponent>(uid, out var powerConsumer))
|
|
powerConsumer.DrawRate = 1; // this needs to be not 0 so that the visuals still work.
|
|
if (TryComp<ApcPowerReceiverComponent>(uid, out var apcReceiver))
|
|
apcReceiver.Load = 1;
|
|
PowerOff(uid, component);
|
|
UpdateAppearance(uid, component);
|
|
}
|
|
|
|
public void SwitchOn(EntityUid uid, EmitterComponent component)
|
|
{
|
|
component.IsOn = true;
|
|
if (TryComp<PowerConsumerComponent>(uid, out var powerConsumer))
|
|
powerConsumer.DrawRate = component.PowerUseActive;
|
|
if (TryComp<ApcPowerReceiverComponent>(uid, out var apcReceiver))
|
|
{
|
|
apcReceiver.Load = component.PowerUseActive;
|
|
PowerOn(uid, component);
|
|
}
|
|
// Do not directly PowerOn().
|
|
// OnReceivedPowerChanged will get fired due to DrawRate change which will turn it on.
|
|
UpdateAppearance(uid, component);
|
|
}
|
|
|
|
public void PowerOff(EntityUid uid, EmitterComponent component)
|
|
{
|
|
if (!component.IsPowered)
|
|
{
|
|
return;
|
|
}
|
|
|
|
component.IsPowered = false;
|
|
|
|
// Must be set while emitter powered.
|
|
DebugTools.AssertNotNull(component.TimerCancel);
|
|
component.TimerCancel?.Cancel();
|
|
|
|
UpdateAppearance(uid, component);
|
|
}
|
|
|
|
public void PowerOn(EntityUid uid, EmitterComponent component)
|
|
{
|
|
if (component.IsPowered)
|
|
{
|
|
return;
|
|
}
|
|
|
|
component.IsPowered = true;
|
|
|
|
component.FireShotCounter = 0;
|
|
component.TimerCancel = new CancellationTokenSource();
|
|
|
|
Timer.Spawn(component.FireBurstDelayMax, () => ShotTimerCallback(uid, component), component.TimerCancel.Token);
|
|
|
|
UpdateAppearance(uid, component);
|
|
}
|
|
|
|
private void ShotTimerCallback(EntityUid uid, EmitterComponent component)
|
|
{
|
|
if (component.Deleted)
|
|
return;
|
|
|
|
// Any power-off condition should result in the timer for this method being cancelled
|
|
// and thus not firing
|
|
DebugTools.Assert(component.IsPowered);
|
|
DebugTools.Assert(component.IsOn);
|
|
|
|
Fire(uid, component);
|
|
|
|
TimeSpan delay;
|
|
if (component.FireShotCounter < component.FireBurstSize)
|
|
{
|
|
component.FireShotCounter += 1;
|
|
delay = component.FireInterval;
|
|
}
|
|
else
|
|
{
|
|
component.FireShotCounter = 0;
|
|
var diff = component.FireBurstDelayMax - component.FireBurstDelayMin;
|
|
// TIL you can do TimeSpan * double.
|
|
delay = component.FireBurstDelayMin + _random.NextFloat() * diff;
|
|
}
|
|
|
|
// Must be set while emitter powered.
|
|
DebugTools.AssertNotNull(component.TimerCancel);
|
|
Timer.Spawn(delay, () => ShotTimerCallback(uid, component), component.TimerCancel!.Token);
|
|
}
|
|
|
|
private void Fire(EntityUid uid, EmitterComponent component)
|
|
{
|
|
if (!TryComp<GunComponent>(uid, out var gunComponent))
|
|
return;
|
|
|
|
var xform = Transform(uid);
|
|
var ent = Spawn(component.BoltType, xform.Coordinates);
|
|
var proj = EnsureComp<ProjectileComponent>(ent);
|
|
_projectile.SetShooter(ent, proj, uid);
|
|
|
|
var targetPos = new EntityCoordinates(uid, new Vector2(0, -1));
|
|
|
|
_gun.Shoot(uid, gunComponent, ent, xform.Coordinates, targetPos, out _);
|
|
}
|
|
|
|
private void UpdateAppearance(EntityUid uid, EmitterComponent component)
|
|
{
|
|
EmitterVisualState state;
|
|
if (component.IsPowered)
|
|
{
|
|
state = EmitterVisualState.On;
|
|
}
|
|
else if (component.IsOn)
|
|
{
|
|
state = EmitterVisualState.Underpowered;
|
|
}
|
|
else
|
|
{
|
|
state = EmitterVisualState.Off;
|
|
}
|
|
_appearance.SetData(uid, EmitterVisuals.VisualState, state);
|
|
}
|
|
|
|
private void OnSignalReceived(EntityUid uid, EmitterComponent component, ref SignalReceivedEvent args)
|
|
{
|
|
// must anchor the emitter for signals to work
|
|
if (TryComp<PhysicsComponent>(uid, out var phys) && phys.BodyType != BodyType.Static)
|
|
return;
|
|
|
|
if (args.Port == component.OffPort)
|
|
{
|
|
SwitchOff(uid, component);
|
|
}
|
|
else if (args.Port == component.OnPort)
|
|
{
|
|
SwitchOn(uid, component);
|
|
}
|
|
else if (args.Port == component.TogglePort)
|
|
{
|
|
if (component.IsOn)
|
|
{
|
|
SwitchOff(uid, component);
|
|
}
|
|
else
|
|
{
|
|
SwitchOn(uid, component);
|
|
}
|
|
}
|
|
else if (component.SetTypePorts.TryGetValue(args.Port, out var boltType))
|
|
{
|
|
component.BoltType = boltType;
|
|
}
|
|
}
|
|
}
|
|
}
|