mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-18 14:07:53 +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)
269 lines
10 KiB
C#
269 lines
10 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Server.Cargo.Systems;
|
|
using Content.Server.Emp;
|
|
using Content.Shared.Emp;
|
|
using Content.Server.Power.Components;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Rejuvenate;
|
|
using Content.Shared.Timing;
|
|
using JetBrains.Annotations;
|
|
using Robust.Shared.Containers;
|
|
using Robust.Shared.Utility;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Server.Power.EntitySystems
|
|
{
|
|
[UsedImplicitly]
|
|
public sealed class BatterySystem : EntitySystem
|
|
{
|
|
[Dependency] protected readonly IGameTiming Timing = default!;
|
|
|
|
[Dependency] private readonly SharedContainerSystem _containers = default!; // WD EDIT
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<ExaminableBatteryComponent, ExaminedEvent>(OnExamine);
|
|
SubscribeLocalEvent<PowerNetworkBatteryComponent, RejuvenateEvent>(OnNetBatteryRejuvenate);
|
|
SubscribeLocalEvent<BatteryComponent, RejuvenateEvent>(OnBatteryRejuvenate);
|
|
SubscribeLocalEvent<BatteryComponent, PriceCalculationEvent>(CalculateBatteryPrice);
|
|
SubscribeLocalEvent<BatteryComponent, EmpPulseEvent>(OnEmpPulse);
|
|
|
|
SubscribeLocalEvent<NetworkBatteryPreSync>(PreSync);
|
|
SubscribeLocalEvent<NetworkBatteryPostSync>(PostSync);
|
|
}
|
|
|
|
private void OnNetBatteryRejuvenate(EntityUid uid, PowerNetworkBatteryComponent component, RejuvenateEvent args)
|
|
{
|
|
component.NetworkBattery.CurrentStorage = component.NetworkBattery.Capacity;
|
|
}
|
|
|
|
private void OnBatteryRejuvenate(EntityUid uid, BatteryComponent component, RejuvenateEvent args)
|
|
{
|
|
SetCharge(uid, component.MaxCharge, component);
|
|
}
|
|
|
|
private void OnExamine(EntityUid uid, ExaminableBatteryComponent component, ExaminedEvent args)
|
|
{
|
|
if (!TryComp<BatteryComponent>(uid, out var batteryComponent))
|
|
return;
|
|
if (args.IsInDetailsRange)
|
|
{
|
|
var effectiveMax = batteryComponent.MaxCharge;
|
|
if (effectiveMax == 0)
|
|
effectiveMax = 1;
|
|
var chargeFraction = batteryComponent.CurrentCharge / effectiveMax;
|
|
var chargePercentRounded = (int) (chargeFraction * 100);
|
|
args.PushMarkup(
|
|
Loc.GetString(
|
|
"examinable-battery-component-examine-detail",
|
|
("percent", chargePercentRounded),
|
|
("markupPercentColor", "green")
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
private void PreSync(NetworkBatteryPreSync ev)
|
|
{
|
|
// Ignoring entity pausing. If the entity was paused, neither component's data should have been changed.
|
|
var enumerator = AllEntityQuery<PowerNetworkBatteryComponent, BatteryComponent>();
|
|
while (enumerator.MoveNext(out var netBat, out var bat))
|
|
{
|
|
DebugTools.Assert(bat.CurrentCharge <= bat.MaxCharge && bat.CurrentCharge >= 0);
|
|
netBat.NetworkBattery.Capacity = bat.MaxCharge;
|
|
netBat.NetworkBattery.CurrentStorage = bat.CurrentCharge;
|
|
}
|
|
}
|
|
|
|
private void PostSync(NetworkBatteryPostSync ev)
|
|
{
|
|
// Ignoring entity pausing. If the entity was paused, neither component's data should have been changed.
|
|
var enumerator = AllEntityQuery<PowerNetworkBatteryComponent, BatteryComponent>();
|
|
while (enumerator.MoveNext(out var uid, out var netBat, out var bat))
|
|
{
|
|
SetCharge(uid, netBat.NetworkBattery.CurrentStorage, bat);
|
|
}
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
var query = EntityQueryEnumerator<BatterySelfRechargerComponent, BatteryComponent>();
|
|
while (query.MoveNext(out var uid, out var comp, out var batt))
|
|
{
|
|
if (!comp.AutoRecharge) continue;
|
|
if (batt.IsFullyCharged) continue;
|
|
|
|
if (comp.AutoRechargePause)
|
|
{
|
|
if (comp.NextAutoRecharge > Timing.CurTime)
|
|
continue;
|
|
}
|
|
|
|
SetCharge(uid, batt.CurrentCharge + comp.AutoRechargeRate * frameTime, batt);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the price for the power contained in an entity's battery.
|
|
/// </summary>
|
|
private void CalculateBatteryPrice(EntityUid uid, BatteryComponent component, ref PriceCalculationEvent args)
|
|
{
|
|
args.Price += component.CurrentCharge * component.PricePerJoule;
|
|
}
|
|
|
|
private void OnEmpPulse(EntityUid uid, BatteryComponent component, ref EmpPulseEvent args)
|
|
{
|
|
args.Affected = true;
|
|
args.Disabled = true;
|
|
UseCharge(uid, args.EnergyConsumption, component);
|
|
// Apply a cooldown to the entity's self recharge if needed to avoid it immediately self recharging after an EMP.
|
|
TrySetChargeCooldown(uid);
|
|
}
|
|
|
|
public float UseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
|
{
|
|
if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0)
|
|
return 0;
|
|
|
|
var newValue = Math.Clamp(0, battery.CurrentCharge - value, battery.MaxCharge);
|
|
var delta = newValue - battery.CurrentCharge;
|
|
battery.CurrentCharge = newValue;
|
|
|
|
// Apply a cooldown to the entity's self recharge if needed.
|
|
TrySetChargeCooldown(uid);
|
|
|
|
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
|
RaiseLocalEvent(uid, ref ev);
|
|
return delta;
|
|
}
|
|
|
|
public void SetMaxCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
|
{
|
|
if (!Resolve(uid, ref battery))
|
|
return;
|
|
|
|
var old = battery.MaxCharge;
|
|
battery.MaxCharge = Math.Max(value, 0);
|
|
battery.CurrentCharge = Math.Min(battery.CurrentCharge, battery.MaxCharge);
|
|
if (MathHelper.CloseTo(battery.MaxCharge, old))
|
|
return;
|
|
|
|
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
|
RaiseLocalEvent(uid, ref ev);
|
|
}
|
|
|
|
public void SetCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
|
{
|
|
if (!Resolve(uid, ref battery))
|
|
return;
|
|
|
|
var old = battery.CurrentCharge;
|
|
battery.CurrentCharge = MathHelper.Clamp(value, 0, battery.MaxCharge);
|
|
if (MathHelper.CloseTo(battery.CurrentCharge, old))
|
|
return;
|
|
|
|
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
|
RaiseLocalEvent(uid, ref ev);
|
|
}
|
|
/// <summary>
|
|
/// Checks if the entity has a self recharge and puts it on cooldown if applicable.
|
|
/// </summary>
|
|
public void TrySetChargeCooldown(EntityUid uid, float value = -1)
|
|
{
|
|
if (!TryComp<BatterySelfRechargerComponent>(uid, out var batteryself))
|
|
return;
|
|
|
|
if (!batteryself.AutoRechargePause)
|
|
return;
|
|
|
|
// If no answer or a negative is given for value, use the default from AutoRechargePauseTime.
|
|
if (value < 0)
|
|
value = batteryself.AutoRechargePauseTime;
|
|
|
|
if (Timing.CurTime + TimeSpan.FromSeconds(value) <= batteryself.NextAutoRecharge)
|
|
return;
|
|
|
|
SetChargeCooldown(uid, batteryself.AutoRechargePauseTime, batteryself);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Puts the entity's self recharge on cooldown for the specified time.
|
|
/// </summary>
|
|
public void SetChargeCooldown(EntityUid uid, float value, BatterySelfRechargerComponent? batteryself = null)
|
|
{
|
|
if (!Resolve(uid, ref batteryself))
|
|
return;
|
|
|
|
if (value >= 0)
|
|
batteryself.NextAutoRecharge = Timing.CurTime + TimeSpan.FromSeconds(value);
|
|
else
|
|
batteryself.NextAutoRecharge = Timing.CurTime;
|
|
}
|
|
|
|
/// <summary>
|
|
/// If sufficient charge is available on the battery, use it. Otherwise, don't.
|
|
/// </summary>
|
|
public bool TryUseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
|
{
|
|
if (!Resolve(uid, ref battery, false) || value > battery.CurrentCharge)
|
|
return false;
|
|
|
|
UseCharge(uid, value, battery);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Like SetCharge, but checks for conditions like EmpDisabled before executing
|
|
/// </summary>
|
|
public bool TrySetCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
|
{
|
|
if (!Resolve(uid, ref battery, false) || TryComp<EmpDisabledComponent>(uid, out var emp))
|
|
return false;
|
|
|
|
SetCharge(uid, value, battery);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns whether the battery is at least 99% charged, basically full.
|
|
/// </summary>
|
|
public bool IsFull(EntityUid uid, BatteryComponent? battery = null)
|
|
{
|
|
if (!Resolve(uid, ref battery))
|
|
return false;
|
|
|
|
return battery.CurrentCharge / battery.MaxCharge >= 0.99f;
|
|
}
|
|
|
|
// WD EDIT START
|
|
public bool TryGetBatteryComponent(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery,
|
|
[NotNullWhen(true)] out EntityUid? batteryUid)
|
|
{
|
|
if (TryComp(uid, out battery))
|
|
{
|
|
batteryUid = uid;
|
|
return true;
|
|
}
|
|
|
|
if (!_containers.TryGetContainer(uid, "cell_slot", out var container)
|
|
|| container is not ContainerSlot slot)
|
|
{
|
|
battery = null;
|
|
batteryUid = null;
|
|
return false;
|
|
}
|
|
|
|
batteryUid = slot.ContainedEntity;
|
|
|
|
if (batteryUid != null)
|
|
return TryComp(batteryUid, out battery);
|
|
|
|
battery = null;
|
|
return false;
|
|
}
|
|
// WD EDIT END
|
|
}
|
|
}
|